Hello, I’m currently stuck on a basic problem but I can’t seem to find the answer.
I want to run a function when a player spawns for the first time, and every time he respawns.
I think it got something to do with .CharacterAdded but I don’t see how.
Thank you.
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
-- First time player joins
player.CharacterAdded:Connect(function(char)
-- when their character loads in
end)
end)
local PlayerService = game:GetService("Players")
PlayerService.PlayerAdded:Connect(function(player) --the "player" variable will be defined automatically by the player added event
player.CharacterAdded:Connect(function(character) --the "character" variable will be defined automatically by the character added event
--you can do whatever you want here, for example:
--you can kill the player when he spawns
local Humanoid = character:WaitForChild("Humanoid")
Humanoid.Health = 0
end)
end)
Adding to what @TheDCraft wrote, if you wish to skip the first time the player respawns so ignoring when the player join the game I suggest you put player.CharacterAdded:Wait() before the CharacterAdded connection like so:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
-- First time player joins
player.CharacterAdded:Wait()
player.CharacterAdded:Connect(function(char)
-- when their character loads in
end)
end)