Player.CharacterAdded not working

So I was working on a admin panel, and I want a button to appear only if you’re a certain player, so I wrote this script:

Server script in ServerScriptServer

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function()
		if plr.UserId == 786513213 or plr.UserId == 168464656 then
			game.ReplicatedStorage.ccEvent:FireClient(plr)
		end
	end)
end)

For some reason, the gui doesn’t appear when I reset, it works when I join the game, but not when I reset.

Any help is appreciated!

Could you put a print, or maybe 2, in the script, and tell us if both prints after reset?

1 Like

It doesnt work because once the character is added to the game it doesnt add again > use child added and check for character to fix.

1 Like

Turn off the “ResetOnSpawn” property on your ScreenGui.

I can’t because some parts of the gui require that to work properly.

I still need to get the player and the character, if I only get the character, I can’t find the userId of the player. How could I get the player using the new script?

EDIT: I did this and it works now:

game.Players.PlayerAdded:Connect(function(plr)
	workspace.ChildAdded:Connect(function(child)
		for i,v in pairs(child:GetChildren()) do
			if v.Name == "Humanoid" and v:IsA("Humanoid") then
				if plr.UserId == 786513213 or plr.UserId == 168464656 then
					game.ReplicatedStorage.ccEvent:FireClient(plr)
				end
			end
		end
	end)
end)

That is incorrect. The player.CharacterAdded event always fires whenever the character respawns. But I don’t disagree with using the ChildAdded event to detect when a character respawns.

1 Like

Instead of having to make a for i loop and checking if the child has a humanoid, you can just make a simple statement checking if the child that was added has the same name as the player’s name:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ccEvent = ReplicatedStorage:WaitForChild("ccEvent")

game.Players.PlayerAdded:Connect(function(plr)
	workspace.ChildAdded:Connect(function(child)
		-- just check if the child's name is the player's name
		if child.Name == plr.Name then
			-- then check the player's userId
			plr.UserId == 786513213 or plr.UserId == 168464656 then
				-- Fires the event
				ccEvent:FireClient(plr)
			end
		end
	end)
end)
1 Like

Oh you’re right, I just read the wiki link.
Player.CharacterAdded (roblox.com)
I always use child added though I rarely use character added. Its better imo

2 Likes