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

Do you know how to make a variable not change with the script?

So let me set the scene, I'm making a Gui that can affect the Workspace, but I'm not gonna know what the tree is to get to the thing from game, so I need to have it from where the script's origin is. Any idea how to help?

Current Code:

local LR = script.Parent.Parent.Parent.Parent.LegRest
local button = script.Parent

local function test()
	LR.Transparency = 0
	LR.CanCollide = false
end

button.MouseButton1Click:Connect(test)

Tree: https://gyazo.com/ed50f7a0a4e6f5c08dba69af1c75c2d3

Thanks in advanced.

2 comments

0

?

1 answer

✓ Accepted answer

If you don't know how many levels deep you need to go in the ancestry of an object, and assuming you know the name of the object you're searching for, you can use either the :findFirstAncestor() method (if going up), or you could parse through a :getDescendants() array (if going down).

So in your case you could use

local LR = script:findFirstAncestor("LegRest")

I think that's what you're asking. Hope this helps.

3 comments

0

It would work, but it's saying there's an error somewhere else now. Error message: https://gyazo.com/255314c5c1dad768f2b69aab99a002a6 New script: https://gyazo.com/b5bb3c7239780aeb5dc637b86d4d836e

0

It's probably a matter of timing. Move the LR variable definition (Line 1) to the first line inside of the test() function (which would make it go between "local function test()" and "LR.Transparency = 0"

0

Hey. Turns out that didn't work, but luckily I found a better way to do it. I had a RopeConstraint and made the Attachment0 be under the LegRest, so I just had to do script.Parent.Parent.Parent.RopeConstraint.Attachment0.Parent to apply to the LegRest. Thanks for the help anyways!

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