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

How to change an object's parent? (AncestryChanged)

I've been trying to change a SurfaceGui's parent to PlayerGui, can anyone help me out?

Script

local ScreenGui = script.Parent
ScreenGui.Parent = script.Parent.Parent


game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
		if char == true then
        	ScreenGui.AncestryChanged:Connect(function(child, parent)
				print(child.Name.." is now a child of "..parent.Name)
			end)
			ScreenGui.Parent = game.Players.LocalPlayer.PlayerGui
			end
		end)
	end)

2 comments

0

where is the script located

0

The script is in the SurfaceGui

1 answer

✓ Accepted answer

Hello.

Issue:

You cannot use LocalPlayer in a Server/RegularScript as they run on a physical server instead of a client.

Solution:

Use the "plr" parameter instead.

Fixed Script:

local ScreenGui = script.Parent
ScreenGui.Parent = script.Parent.Parent


game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        if char == true then
            ScreenGui.AncestryChanged:Connect(function(child, parent)
                print(child.Name.." is now a child of "..parent.Name)
            end)
            ScreenGui.Parent = plr.PlayerGui
            end
        end)
    end)

1 comment

0

Thank you, I also had to remove the "if char == true" statement.

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