CharacterAdded functions not loading correctly

Your problem is that you’re not taking care of what has already happened or what already exists.

local function CharacterAdded(char)
	--Character added code
end

local function PlayerAdded(player)
	--Connect the event
	player.CharacterAdded:Connect(CharacterAdded)
	
	--Take care of when it already exists
	local char = player.Character
	if char then
		CharacterAdded(char)
	end
end

--Connect the event
game.Players.PlayerAdded:Connect(PlayerAdded)

--Take care of when it already exists
for i,v in pairs(game.Players:GetPlayers()) do
	PlayerAdded(v)
end

This should handle any and all situations for when a player and character get added. Notice how I check if things existed after I connected the event. The event is not going to fire for anything that already exists, so you have to run the function on it instead.

EDIT: Don’t use for i,v in next, since the performance gains from Luau are lost when using next (negligible difference in this case, but still fair to note).

43 Likes