Can I start off a script with Players.CharacterAdded:Connect(function()
or do I have to embed it inside a Players.PlayerAdded:Connect(function()
?
2 Likes
You have to use .PlayerAdded.
The closest to it would be this:
local Players = game:GetService('Players')
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
print(player.Name..'\'s character was added!'
end
end
Ah ok, so I didnât start off with playeradded, it would return nil?
1 Like
It would return an error like âCharacterAdded is not a member of Playersâ.
So long as you have a reference to the Player, you can utilize Player.CharacterAdded
.
For example, you could use it on its own in a LocalScript without nesting it within a Players.PlayerAdded
function:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local function CharacterRespawned(Character)
-- Code
end
player.CharacterAdded:Connect(CharacterRespawned)
if player.Character then
CharacterRespawned(player.Character)
end
More information on the âPlayerâ and âLocalPlayerâ here:
If I wanna have it in a server script, can I do:
local Player = game.Players.LocalPlayer
Player.CharacterAdded:Connect(function()
--blah blah blah
end)
Do I have to reference Players or will local player suffice?
In a server script, you canât get LocalPlayer. You have to use Players.PlayerAdded
1 Like
Oh wow, I never realised that, thanks for telling me.