Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to use string as an object reference?

I have a script for me that allows me to open an interface and type in the location of the part eg.(game.Workspace.BigPart), but i don't know how to use a string value as an object reference. i have tried various methods, including:

input = 'game.Workspace.BigPart'
place = game:FindFirstChild(input)

, but to no avail.

1 answer

✓ Accepted answer

String manipulation

The worst. I promise. You essentially need to split your string at each . - Fortunately, Roblox has a function just for this purpose. Use string.split to produce an array of names.

input = input:split('.')

Then remove the leading game if it's there

if input[1] == 'game' then table.remove(input, 1) end

And finally walk through your array, getting each child along the way

place = game
for _, v in pairs(input) do
  place = place:FindFirstChild(v)
end

And place will be the Instance you referenced. Provided it's valid, of course. If it's not, then your script will error out. Catch this with pcall

This discussion is preserved as it was. New answers, comments, and votes are closed.