Simple script not working, looking for simple solution

Why does line 5 of my script not give the player the Gui? I am willing to provide any information that wasn’t clear in this post.

game.Players.PlayerAdded:Connect(function(player)
game.ServerStorage.Spectate:Clone().Parent = player:WaitForChild(“PlayerGui”)
player:GetPropertyChangedSignal(“Team”):Connect(function()
if player.Team == game.Teams.Lobby then
game.ServerStorage.Spectate:Clone().Parent = player.PlayerGui
else
if player.PlayerGui:FindFirstChild(“Spectate”) then
player.PlayerGui.Spectate:Destroy()
end
end
end)
end)

Try not cloning and setting the parent at the same time.

e.g. replace

game.ServerStorage.Spectate:Clone().Parent = player.PlayerGui

with

local guiClone = game.ServerStorage.Spectate:Clone()
guiClone.Parent = player.PlayerGui

No luck. I even simplified the script a little, but still no luck. It only gives the player the gui the first time, then never again.

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
if player.Team == game.Teams.Lobby then
game.ServerStorage.Spectate:Clone().Parent = player:WaitForChild(“PlayerGui”)
else
if player.PlayerGui:FindFirstChild(“Spectate”) then
player.PlayerGui.Spectate:Destroy()
end
end
end)
end)

Is there anything in the output? It would also help if you could include the indents in your code sample to make the code more readable.

Nope, nothing in output. Also devforum won’t let me format the script for some reason, sorry if that makes things harder.

There are two issues I can think of:

  1. Sometimes studio randomly doesn’t detect the PlayerAdded event (don’t ask me why, but I’ve experienced it randomly before). Maybe try testing it in-game and then checking the output?
  2. You might not be receiving anything in the output if the conditional on Line 3 is returning false. Try adding a slight wait(.05) buffer under line 2 just to see if it affects anything.

I fixed the issue. Thanks for your support.

1 Like

Mind letting me see the working script, i’m interested and might use it in the future.