Hey there,
I need a function that runs when a player joins the game. CharacterAdded is only running after I die, but not when I first load in. Is there an event that can solve this?
Thanks for any and all help
Hey there,
I need a function that runs when a player joins the game. CharacterAdded is only running after I die, but not when I first load in. Is there an event that can solve this?
Thanks for any and all help
game.Players.PlayerAdded
This runs once the players joins (adds) to the game.
If I die, will it run when I respawn too?
If you want to trigger a function when you die youâd use Humanoid.Died
It wonât run when you respawn.
Okay, Iâll look into it. Thanks so much for replying!
For this youâll be using Players.PlayerAdded
event, which will give you the player
parameter being the Player
instance object. Just like this:
game:GetService("Players").PlayerAdded:Connect(function(player)
... -- code
end)
Now, if you want something for this and do the ârespawningâ thing since this will only fire once the player joins, youâll be doing such as this:
game:GetService("Players").PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
... -- code
end)
end)
And there you go! Basically CharacterAdded
it is fired everytime the player is being removed and inserted into the workspace
again.
You donât have to include :GetService, but itâs fine if you do. Just to shorten the code.
Consistency in code! It is better to have this type of practice, such as if you were doing :IsA("Class")
instead of :IsA'Class'
youâll be using the ()
to keep the consistency and readibility of your code.
ok so when you write the function for Humanoid.Died, how do you do it? Because every single time I try it wonât catch it or fire.
Hey guys
The reason it wasnât firing was because I needed to play it in the app, not in studio (it had to do with saving progress and all that). Thanks for helping me work through this to find the solution!