Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Why can't I use a variable to reference a brick in a model?

Asked by
Seyfert 90
7 years ago
Edited 7 years ago

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
0
You're trying to retrieve a decal from a number value. (5.Decal.Texture) lol TheeDeathCaster 2368 — 7y
0
That's a typo on here, if you had read I am asking about specifically number 9, why doesn't that work? nextBrick is being converted into a string so shouldn't that work? Seyfert 90 — 7y
0
No, you're trying to retrieve an object from a number, not an object; it's like saying "i iz string".Parent = nil. However, you could use the FindFirstChild function again to try and find it by the number if the name's a number, or get the position from a table from the parent object. (obj:GetChildren()[Number]) TheeDeathCaster 2368 — 7y
0
I am not folllowing, sorry. So, is it line 9 where my issue is at? My thinking is, v.Name + z turns into a number because Roblox recognizes what I want to do. Then I change it to a string so I can navigate for example, 3.Decal.Texture but its doing nextBrick.Decal.Texture instead. So you recommend using FindFirstChild? How though? I do not understand. FindFirstChild("3") if the line V.name + z = 3 Seyfert 90 — 7y

1 answer

Log in to vote
0
Answered by
cabbler 1942 Moderation Voter
7 years ago
Edited 7 years ago

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
0
I was trying to keep my code simple, z is the variable I used for a for z =1, 10 loop but i did not include it here because i was trying to keep it simple. Sorry about that. I do not underdstand linr 13. Why do i need (num <= #children and #num) or 1)] cant i just simply have model[tostring(num)] ? Seyfert 90 — 7y
0
Hmm that did not work, I understand now that what I am trying to do is wrong. How can I go about doing what I want to do? Seyfert 90 — 7y
0
I assumed nextBrick was the brick named by the current brick name + 1, or default to 1 if not possible. The operation does that. I don't know how you'd go about it since you gave no description besides that there is an error. I recommend you fix what you can then ask a better question. cabbler 1942 — 7y
Ad

Answer this question