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

How do I properly reference a path from a variable in script?

Asked by 4 years ago
Edited 4 years ago

I can’t really explain it in the title, but you should hopefully be able to see what I’m trying to do

if tilepath ~= nil then
     local clonetile = tiles[tostring(tilepath)]:Clone()

The tilepath is grabbed from a table I have Table:

    ["rocksvariant1"] = {
        ['chance'] = 20,
        ['path'] = 'RocksVariant1'

The script knows what tiles are Reference:

local tiles = script:WaitForChild("ProceduralForestTiles"):GetChildren()

and If I print the tilepath variable i get Output:

Rocksvariant1

So how would I use the variable as a path, I’m trying it the way I have it now but I’m getting this error

[05:17:28.106 - Workspace.CurrentMap.ProceduralGeneratorScript:134: attempt to index nil with 'Clone'

Basically im trying to sort of make it say “tiles.RocksVariant1:Clone” Without it actually saying that.

Thanks.

1 answer

Log in to vote
0
Answered by 4 years ago

The problem with the code is the fact that you get the children of the tiles object. The reason this does not work is because you are trying to index an array with a dictionary key. So your new tiles variable would look like:

local tiles = script:WaitForChild("ProceduralForestTiles") 

Whenever I do something like this in my scripts, I always use object:FindFirstChild() and check to see if it is actually there. What I would also suggest:

local clonetile = tiles:FindFirstChild(tilepath)
if clonetile then 
    local clone = clonetile:Clone()
end

You could also get rid of your intial if statement in this situation.

Hope this helps!

0
local clonetile = tiles:FindFirstChild(tilepath) Is giving me the error 13:07:37.006 - Workspace.CurrentMap.ProceduralGeneratorScript:132: attempt to call a nil value, I'm assuming this means tilepath is indeed nil but when it prints the output its all good RedCoatOracuda 35 — 4y
0
No the error means that the tiles variable is indeed nil. Check to see if your tiles variable is ok. Sir_Ragealot 72 — 4y
0
Find first child does not error unless you try to use the child before checking if it is actually there. Your problem is that the object that calls find first child is not there Sir_Ragealot 72 — 4y
Ad

Answer this question