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

How to change the parent of a script?

I've been trying to make a key open a door and I got help for it, but it didn't work for me. I'm trying to change the parent of a script but whenever I actually do it, the script stops working once the parent has changed or has been enabled....

	game.StarterGui.Key1.Keyhole.Parent = game.Workspace

Any help is appreciated. Thanks

2 comments

0

it it a gui or a script?

0

It's a script

2 answers

you have to reenable the script you changed the parent, because it probably is trying to use a variable you have set before moving that script. To reenable you make "script.Disabled= true" then "script.Disabled = false". I don't think why move the script though

4 comments

0

Also if your script has a variable that is set using the own script as reference "door = script.Parent.Parent", you have to take into account the variable will change when you reenable it

0

Dude if you disable the script and try to enable it again, it won't work. The script will just stay disabled

0

Yeah I tried that earlier-

0

You need to use a remote event or something to fire to another script. Use that other script to disable the script, re-parent it, then enable it.

That's because when a player play the game, the StarterGui's script is not actually the one that is used. The one that is used is inside Players > LocalPlayer > PlayerGui > Key1 > Keyhole > The Script. You can try to play the game in studio and see if that is true.

So instead of doing:

game.StarterGui.Key1.Keyhole.Parent = game.Workspace

You can do:

game:GetService("Players").PlayerAdded:Connect(function(Player)
	game:GetService("Players"):WaitForChild(Player.Name):WaitForChild("PlayerGui"):WaitForChild("Key1"):WaitForChild("Keyhole"):WaitForChild("Script").Parent = workspace
end)

1 comment

0

workspace & game.Workspace is the same, if you don't wanna type game.Workspace, then the "w" for workspace must be lower case

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