I have a model with bricks named as numbers like so (1-5 not in order on purpose)
-----------------Model
--------------2
--Decal
--------------3
--Decal
--------------1
--Decal
--------------4
--Decal
--------------5
--Decal
Don't worry too much about the code being correct, is just that Roblox keeps giving me
"attempt to index field 'Decal' (a nil value)" for line 9. Why is it doing this?
local chosenTexture = game.Workspace.ReferenceDecal local model = game.Workspace.Board:GetChildren() for i, v in pairs(model) do local decal = v:FindFirstChild("Decal") if decal then if decal.Texture == chosenTexture.Texture then v.Decal.Texture = "" ---Moves the decal in current brick to next in ascending order local nextBrick = v.Name + z ---Ik I am adding a string to int but it works local decalPlaceHolder = tostring(nextBrick).Decal.Texture game.Workspace.Board.tostring(nextBrick).Decal.Texture = "decalPlaceHolder" end end end
There is no reason to use tostring
in those last lines. Strings have no hierarchy to index so the script errors. You want to do a proper string index or set nextBrick to an object.
Also I'm not sure what "v.Name + z" is meant to do. What is z? There is no way it doesn't error like you say. If you really want the next item in the model you would need a more complex operation.
No idea what your script is meant to do but I tried.
local chosenTexture = workspace.ReferenceDecal --reference the model to make things easier local model = workspace.Board local children = model:GetChildren() for i, v in pairs(children) do local decal = v:FindFirstChild("Decal") if decal then if decal.Texture == chosenTexture.Texture then v.Decal.Texture = "" local num = tonumber(v.Name) + 1 --use [] to index by string --defaults to 1 if over 5 local nextBrick = model[tostring( (num <= #children and num) or 1 )] local decalPlaceHolder = nextBrick.Decal.Texture nextBrick.Decal.Texture = "decalPlaceHolder" end end end