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

Why can I not add stuff to the PlayerGui upon respawning?

Asked by
Kami_Yo 72
5 years ago
Edited 5 years ago

I am having this weird issue: I can store a GUI from the ReplicatedStorage into the player's PlayerGui on Start Up, but I can't store any GUI when respawning? Is there something I am missing here as to why I can't do this?

I can't even put a folder into the playerGui upon respawning, why is that? Am I even able to add stuff to the PlayerGui upon respawning or is it not allowed?

And why is it that when I do the same but add it to the player Backpack, it works? Why does it not work specifically for PlayerGui?

Example, this works:

game.Players.PlayerAdded:Connect(function(player) 
    player.CharacterAppearanceLoaded:Connect(function(char)

        local item = Instance.new("Folder")
        item.Name = "Whatever"
        item.Parent = player:WaitForChild("Backpack")

But this doesn't:

game.Players.PlayerAdded:Connect(function(player) 
    player.CharacterAppearanceLoaded:Connect(function(char)

        local item = Instance.new("Folder")
        item.Name = "Whatever"
        item.Parent = player:WaitForChild("PlayerGui")

I don't understand this, why can't I just add to PlayerGui?

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

The reason you can not add to the playergui is because the PlayerGui is client sided, you are trying to access it via server. Also here is a good guide to help you with the issue you are having

So create a localscript in game.StarterPlayer.StarterPlayerScripts

Code:


local Player = game.Players.LocalPlayer local PlayerGui = Player:WaitForChild("PlayerGui") local item = Instance.new("Folder") item.Name = "Whatever" item.Parent = PlayerGui

Look up Remote Events to learn more below.

If you wish to do it via when a player respawns then it can get a little more complicating.

To do this you can do as follows

a Script in game.ServerScriptService


local RespawnEvent = Instance.new("RemoteEvent") RespawnEvent.Name = "RespawnEvent" RespawnEvent.Parent = game.ReplicatedStorage game.Players.PlayerAdded:Connect(function(Player) Player.CharacterAdded:Connect(function(Character) local Humanoid = Character:FindFirstChild("Humanoid") if Humanoid then Humanoid.Died:Connect(function() RespawnEvent:FireClient(Player) end) end end) end)

In a localscript in game.StarterPlayer.StarterPlayerScripts



local RespawnEvent = game.ReplicatedStorage:WaitForChild("RespawnEvent") RespawnEvent.OnClientEvent:Connect(function(Player) local PlayerGui = Player:WaitForChild("PlayerGui") local item = Instance.new("Folder") item.Name = "Whatever" item.Parent = PlayerGui end)
0
Thank you. I figured I had to use RemoteEvents for this. I had a weird workaround before but now it's causing this issue. I knew I'd eventually have to use RemoteEvents. Thank you. At least now I know what the issue is, before I had no idea. Kami_Yo 72 — 5y
Ad

Answer this question