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

Setting the parent of an object doesn't work?

Asked by
vkax 85
6 years ago

So this is my code so far

game.Players.PlayerAdded:connect(function(player)
player:WaitForChild("PlayerGui")
game.ServerStorage.FE.ChangeParent:FireServer(game.ServerStorage.GUIs.Intro:Clone(),player.PlayerGui)   
end)

That script basically gives the GUI to the player and it's FE so yeah, that's why

local ChangeParent = game.ServerStorage.FE.ChangeParent

ChangeParent.OnServerEvent:connect(function(object,parent)
    object.Parent = parent
end)

This is the remote event and the thing for it and I get this error when I do

19:47:26.843 - Something unexpectedly tried to set the parent of trycode to Intro while trying to set the parent of trycode. Current parent is Players.

Can you help me? :)

2 answers

Log in to vote
0
Answered by 6 years ago

PLEASE READ THE WHOLE ANSWER PLEASE. THIS INCLUDES A FEW CHANGES IN YOUR HIERARCHY!

A minor mistake that you did is not include the player parameter. This is why the errorstates your username. The script thinks that your object is the player. Here is the revised part.

Another mistake that you have done is that you put your ChangeParent Remote Event in ServerStorage. This is a wrong thing to do because only the servercan access the ServerStorageservice. Put it in ReplicatedStorage so both the clientand servercan access it

Make sure the script below is a Local Script

local ChangeParent = game.ReplicatedStorage.FE.ChangeParent

ChangeParent.OnServerEvent:connect(function(player,object,parent)
    object.Parent = parent
end)

You did this part correctly Make sure the script below is a Server Script

game.Players.PlayerAdded:connect(function(player)
    player:WaitForChild("PlayerGui")
    game.ReplicatedStorage.FE.ChangeParent:FireServer(
             game.ReplicatedStorage.GUIs.Intro:Clone(),
             player.PlayerGui
        )   
end)
0
Why should the first script be a local script if it's a server event? vkax 85 — 6y
Ad
Log in to vote
0
Answered by
dyler3 1510 Moderation Voter
6 years ago
Edited 6 years ago

Simple mistake here. The only problem that I can see is that you are using incorrect parameters for the OnServerEvent event. The first argument is always the player whose client is invoking the event. So your code should look something more like this:

local ChangeParent = game.ServerStorage.FE.ChangeParent

ChangeParent.OnServerEvent:connect(function(player, object, parent)
    object.Parent = parent
end)

As you can see, all I did was add the 'player' argument into the function. It should now work fine. If not or if you have any further questions let me know and I'll see what I can do. Hope this helps :)

0
Clients don't invoke events they fire them. User#19524 175 — 6y

Answer this question