While I was inside of Roblox Studio, I was making a script like the one before you:
local amount = 123
local Module = require(game.ServerScriptService:WaitForChild("Module"))
script.Parent.ClickDetector.MouseClick:Connect(function(player)
if player.leaderstats.Currency.Value >= amount then
player.leaderstats.Currency.Value = player.leaderstats.Currency.Value - amount
local pet = Module.chooseRandomPet()
--> print(pet.Name.." selected") <--
end
end)
When I went to use it I was greeted with "attempt to concatenate nil with string" around the area with the arrows.
What would I do in order to fix this hindrance.
Module.chooseRandomPet() returns a table that has no field "Name". I'd guess that you get a simple Lua table where you expect to get a Roblox Instance. Or for some reason you managed to assign nil to that Instance's Name property.
Either way you should make find out why you don't what you expect. I found various petModule problems online. All of them had a lot of code in common that was full of mistakes.
If you cannot make sure that you don't get what you expect you should at least handle that case properly by checking if it's a nil value you're about to concatenate.
Related
I'm attempting to mod a game I've been playing recently, and I've encountered an error when writing some code that adds a new mechanic. The problem itself isn't very complicated, but I can't seem to figure out what the solution is in this context. I'm pretty new to coding, especially with lua, so I apologize if the solution is really obvious. I've looked around for some answers, but again, nothing I can find seems to help me in this specific context. The game I'm trying to mod is called Project Zomboid, if that helps.
When attempting to start the game with the mod enabled, the error "Object tried to call nil in isBloodthirsty" pops up. Here's the snippet of code that's causing the error:
local function isBloodthirsty(player)
if player:getDescriptor():getTrait() ~= "bloodthirsty" then
return false else
return true
end
end
Ignoring how poorly written it is, the code was supposed to check if the player had a certain trait, and if so, then set the value isBloodthirsty to true. I think the error is caused by lua not recognizing the value "bloodthirsty", but I'm not sure what I should put instead. If anybody has an idea of what I'm doing wrong, I'd greatly appreciate some help. If it's helpful, I can post the rest of the code.
Thanks to all the great help from the stack overflow community, I managed to figure out what my problem was. The code I had written wasn't working because lua didn't recognize "bloodthirsty" as a valid trait string. My solution was to mix up the code a bit and frame the trait as a profession instead (a profession is kind of like a collection of traits within the game). The following code worked:
local function bloodthirstyStart(player)
if player:getDescriptor():getProfession() ~= "bloodthirsty" then
return
end
if item:IsA("BasePart") then
item.Color = Color3.new(1,0,0)
I want to make a mouse raycast to place colors on parts and it shows me this error.
Error:attempt to index nil with IsA
I tried to change the script a bit and it ended up not working anymore.
the variable "item" needs to be an Instance (example a part or object)
the error is saying "item" is not an Instance please provide more information so we can help you : )
I am working on a game in Roblox and need a little help finding objects with attribute values. It is a tycoon for anyone here familiar with Roblox. I would like to know how you can find all instances with say the attribute Unlock_ID: 1, and then when you press the corresponding button, it will unlock it, or make it show up on the screen. I know how to make the objects unlock, I just don't know how to get all the objects with that attribute.
To put it more simply:
I want to find all the objects/instances inside a section with a specific attribute and a specific value and put them inside a table.
I hope this makes sense.
You can use a for i,v in ipairs loops along with the Folder:GetChildren() as your iteration directory. Then you can insert the instance (using table.insert(table,element) into a table if their part:GetAttribute('Unlock_ID') is equal to 1. To achieve this, simply do the following:
local partsWithAttribute = {}
local folder = workspace.Folder -- change this to whatever path you want to iterate through
for i,part in ipairs(folder:GetChildren()) do
if part:GetAttribute('Unlock_ID') == 1 then
table.insert(partsWithAttribute,part)
end
end
I'm trying, to wrap my head around Touchosc and script based on LUA 5.1.
I have a number of labels, called song0, song1, song2, and so on. I'm trying to set different values in these, using
local text = 'Smoke On The Water'
for i = 1, 2 do
self.children.pager1.children.main.children.song[i].values.text = text
end
but that gives me an error.
:-) I do need help.
Finn
Since you haven't provided the actual error, it's difficult to say what the problem is, but if I have to venture a guess, then try replacing ...children.song[i].values... with ...children["song"..i].values.... Since there is no table song, you just need to generate dynamic field names.
So I just want to insert a value into a table I defined, but it doesn't seem to be working at all.
If I use this code:
t = {1,2,3}
table.insert(t, 9)
all i get is
LuaInterface.LuaScriptException: [string "main"]:2: attempt to call
field 'insert' (a nil value)
Is it possible that I'm using a compiler that just doesn't have this function? I really don't get it. I'm using the BizHawk Emulator for compiling right now if that matters.
Does by chance get "table" or "table.insert" get overwritten somewhere previously in the code? Or maybe the table library was not loaded?
Try
require("table")
prior to the call to table.insert and see if it works then