CharacterAdded in a LocalScript

  1. What do you want to achieve?
    I would like trigger an event in a localscript when a player’s character spawns. I want it to trigger for all the players, not only the localplayer.
  2. What is the issue?
    Player.CharacterAdded doesn’t seem to work in a localscript.
  3. What solutions have you tried so far?
    I’ve tried using workspace.ChildAdded() and look for the name of the instance in the players list and get his HumanoidRootPart but it doesn’t seem to find it.
4 Likes

Player.ChildAdded, fires when an object is parented to the player, not when the player spawns.
Use this: Players | Documentation - Roblox Creator Hub

Also is there a reason you can’t do this from the server?

1 Like

Sorry, i meant CharacterAdded. Let me edit.

Where is the LocalScript? Unless it is in PlayerScripts, it may load after the character. Most places reset on death.

1 Like

It’s in the PlayerGui. And I need it to be in a localscript as I want to make changes on the client only.

Could you detect players being added from the server, then use a remote event to call a function in a local script on the client.

Could we see some of the code you wrote and anything notable you found in the output? It could help us understand the issue better.

I think this should work. I will test it once I get on the computer.

you dont need to use an event in a localscript since localscripts run every time your character gets added. You can just write in the beginning:

local character = localPlr.Character or localPlr.CharacterAdded:Wait()
2 Likes

Use a Wait() method at the end of your event and assign a variable.

This method returns the parameter(s) of the event you use it on, and is highly recommended if you only intend to get the parameters of a particular event, instead of actually writing an entire event and connect it to a function, then assigning a variable.

This is the answer; if you only want the character, this is how you find it in a LocalScript that resets on death.

Isn’t this only for the LocalPlayer? I want it to trigger for every player.

yes, every it will work for every player since every player runs the local script.

Let me explain what I’m trying to do. The LocalPlayer has a boolean value.
I want to do something locally to every players’ character spawning when that value is true.

Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
      if plr.Name ~= localPlayer.Name then
        if value == true then
            for _, v in pairs(char:WaitForChild("HumanoidRootPart"):GetChildren()) do
                -- Doing stuff to the childs of HumanoidRootPart
            end
       end
     end
  end)
end)

I’m not doing anything to the actual localplayer’s character, but for the other characters.

1 Like

oh, in that case just do
local player = game:GetService(“Players”).LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

for _, plr in pairs(game:GetService(“Players”):GetPlayers()) do
if plr.Value == true then – or wherever the value is
for _, v in pairs(plr.Character.HumanoidRootPart:GetChildren()) do

end
end
end

This runs only once right? I have done something simillar for when you press a button. But if the player respawns the things I have done go back to normal, or when another player joins. I need to somehow do the action for every player that spawns.

local function update()
print("Test")
for _, plr in pairs(game:GetService(“Players”):GetPlayers()) do
if plr.Value == true then -- or wherever the value is
for _, v in pairs(plr.Character.HumanoidRootPart:GetChildren()) do

end
end
end
end

update()

game:GetService("Players").PlayerAdded:Connect(function(plr)
if not plr.Character then
plr.CharacterAdded:Wait()
end
update()
end)
1 Like

I tried this and only put a print inside the function but it doesn’t seem to print anything when I respawn so I assume it wouldn’t happen even if another player spawns.

is the value you made set to false?

I literally left the function empty besides the print, didn’t even check the value.