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.
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.
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)
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