Cannot add a GUI to the playerGui folder from a server script

Any Idea why this isn’t working?
I’m trying to create a surface gui set to tiles every time a player respawns.

It’s not throwing any errors, and I’ve tried adding print statements to see if anything doesn’t exist, but everything works fine.

When a player respawns nothing is added to the gui folder.
Any ideas why this is happening?
This is in a server script if that changes anything

for i, player in pairs(players) do
	if player then
		local character = player.Character or player.CharacterAdded:Wait()
		local humanoid = character:WaitForChild("Humanoid")
		resetGUIConnection = humanoid.Died:Connect(function()
			resetGUI2 = player.CharacterAdded:Connect(function()
				local newGUI
				for i, tile in pairs(tiles) do
					newGUI = sGUI:Clone()
					newGUI.Parent = player:WaitForChild("PlayerGui"):WaitForChild("TileEffectFolder")
					newGUI.Adornee = tile
				end
				resetGUI2:Disconnect()
			end)
			character = player.Character or player.CharacterAdded:Wait()
			humanoid = character:WaitForChild("Humanoid")
		end)
	end
end

Oh also this is part of a larger script if you see parts that don’t have a local. The rest was working fine until I added this part to it. It runs fine until the player respawns. That’s when nothing gets added back. This is the part of the code that should add them back in.

I don’t understand why you have the Humanoid involved?

Removing what I see as unnecessary code, this is what is left over:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		for i, tile in pairs(tiles) do
			local newGUI = sGUI:Clone()
			newGUI.Parent = player:WaitForChild("PlayerGui"):WaitForChild("TileEffectFolder")
			newGUI.Adornee = tile
		end
	end)
end)

This may fix your issue, as CharacterAdded is all you need to detect respawning. Otherwise, I’d need to see the full script to see why this isn’t working.

(the code I removed may be necessary, it’s not clear what you are trying to acheive)

1 Like

I replaced it with this, but it still did not fix it.

What I’m trying to achieve is to add a clone of the surface gui to the player gui folder.

The problem is it’s not adding it to the folder although when I added a print statement to it, it printed fine for each iteration of the for loop.

I don’t know why me setting the parent isn’t working.

1 Like

Do you think you could show us your full original code so we can diagnose the problem easier?

local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage:WaitForChild("StartGame")
local tile = replicatedStorage:WaitForChild("Tile")
local sGUI = replicatedStorage:WaitForChild("TileSurface")
local tiles = {}
local resetGUIConnection
local resetGUI2
event.Event:Connect(function(players,sizeOfGrid,position,effect)
	local rowCol = {Row=1,Column=1}
	local newTile
	local newSGUI
	local tileFolder = Instance.new("Folder",workspace)
	tileFolder.Name = ("TileFolder")
	for i, player in pairs(players) do
		if player then
			local character = player.Character or player.CharacterAdded:Wait()
			local humanoid = character:WaitForChild("Humanoid")
			game.Players.PlayerAdded:Connect(function(player)
				local newGUI
				resetGUIConnection = player.CharacterAdded:Connect(function()
					for i, tile in pairs(tiles) do
						newGUI = sGUI:Clone()
						newGUI.Parent = workspace
						newGUI.Adornee = tile
					end
				end)
			end)
		end
	end
	for i=1,sizeOfGrid^2 do
		newTile = tile:Clone()
		newTile.Parent = tileFolder
		for i,player in pairs(players) do
			if player then
				newSGUI = sGUI:Clone()
				newSGUI.Parent = player:WaitForChild("PlayerGui"):WaitForChild("TileEffectFolder")
				newSGUI.Adornee = newTile
			end
		end
		newTile.Position = Vector3.new(position.X + (rowCol.Row*8),position.Y,position.Z + (rowCol.Column*8))
		table.insert(tiles,newTile)
		if rowCol.Row == sizeOfGrid then
			rowCol.Column = rowCol.Column + 1
			rowCol.Row = 1
		else
			rowCol.Row = rowCol.Row + 1
		end
	end
end)

Are you throwing a player added function in an event?? This won’t work as the player has already been added and everything inside the function will not run, you should try placing it outside of that script. Whatever you’re trying to achieve will need to be declared inside of that function.

1 Like

I’m attempting to make it so only the players inside of the given players list argument are getting this connected. Then when they leave the match I can disconnect it. The problem I’m encountering, is that it is not creating a new surface gui and adding to the player, despite me telling it to clone it and add it to the player.

Good point but the problem is that he print debugged it and said it printed the whole way through, so the event must have been fired before the player is added.

@88Swagger88 What the problem might be is that when other players join, the code will execute and those players will receive the GUI instead of the intended player. Maybe remove the event handler for PlayerAdded and see what happens?

Your code seems rather disorganized and overcomplicated, and it is also unclear what information is being passed when the Event is fired. Could you possibly explain what information is being sent when that event is fired and how often that event is fired?

The event is fired to start an match, and it sends the players involved, size of map, and position of the match to make the map at.

The only problem I’m encountering is even though it’s printing just fine, it isn’t creating a new gui. I tried replacing the gui with a block and adding it to the workspace instead, but it isn’t doing that either.

If you see any areas that the code can be improved, can you point it out?

I think you should completely rewrite this portion of your code and think about simplicity. There also should be no need for the PlayerAdded event, as the players are already in the game and you only need to detect when the character is added to add the GUI at that time.

	for i, player in pairs(players) do
		if player then
			local character = player.Character or player.CharacterAdded:Wait()
			local humanoid = character:WaitForChild("Humanoid")
			game.Players.PlayerAdded:Connect(function(player)
				local newGUI
				resetGUIConnection = player.CharacterAdded:Connect(function()
					for i, tile in pairs(tiles) do
						newGUI = sGUI:Clone()
						newGUI.Parent = workspace
						newGUI.Adornee = tile
					end
				end)
			end)
		end
	end
1 Like

resetGui:Disconnect() might be why it wont be adding on respawn. try removing it and see if it will be added upon being respawned.

I removed it and it still doesn’t work. That wasn’t affecting it before though, because I said I did print debugging and all the prints printed to the console correctly.

I’ve done that, however I still need to fix the issue of it not creating the guis. It works fine when you spawn in for the first time, but after that it does not work.

Could you show the code after you’ve rewritten it?

for i, player in pairs(players) do
	if player then
		local character = player.Character or player.CharacterAdded:Wait()
		local humanoid = character:WaitForChild("Humanoid")
		local newGUI
		resetGUIConnection = player.CharacterAdded:Connect(function()
			for i, tile in pairs(tiles) do
				newGUI = sGUI:Clone()
				newGUI.Parent = workspace
				newGUI.Adornee = tile
			end
		end)
	end
end

Are you sure the GUI isn’t being added, or is it just not showing up? Check workspace and see if it’s being cloned into it.

I did and nothing was added to it.

Ok so I changed something, and now it adds to the workspace, but still doesn’t add itself to the player gui when I change workspace to the gui folder. Is this something to do with FE?

newGui.Parent = player.PlayerGui should work?

Also could you show the code after the change you made that fixed the parenting?