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

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

Asked by 5 years ago
Edited 5 years ago

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.

1 answer

Log in to vote
0
Answered by 5 years ago

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.

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 sirgrayknight 4 — 5y
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" ChiefWildin 295 — 5y
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! sirgrayknight 4 — 5y
Ad

Answer this question