CharacterAdded not working with AutoLoads Roblox

I’ve tried different variants of character added and player added but the issue is the same, it will stop functioning. I got lucky once but as soon as I implement character auto loading into it, it breaks. This is the script that’s come the closest and it does autoload the character in but it won’t detect when they’ve died anymore, whereas before I had it it autoload them in it would detect when they died.

Ideally this is meant to give me full control over the player spawning(Character autoload is disabled)
I’ve tried different solutions from the forum but autoload always seems to break them; this is in a server-script located inside workspace.

local Players = game:GetService("Players")

local function onCharacterAdded(character)
	local player = Players:GetPlayerFromCharacter(character)
	print(player.Name.." has spawned")
	character:WaitForChild("Humanoid").Died:Connect(function()
		print(player.Name .. " has died!")

	end)
end
local function onCharacterRemoving(character)
	local player = Players:GetPlayerFromCharacter(character)
	print(player.Name.." is despawning")
	
end

local function onPlayerAdded(player)
	
	player:LoadCharacter()
	player.CharacterAdded:Connect(onCharacterAdded)
	player.CharacterRemoving:Connect(onCharacterRemoving)
end


Players.PlayerAdded:Connect(onPlayerAdded)

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.

Although you should probably not 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).

1 Like

Can you elaborate on this please? I don’t understand the point of it, though I do understand there is a point to it. Thank you though, with some modification this is nicely functional for my requirements.

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

The script I ended up using in the end(A minor modification of Grenade’s script, if anyone does come across this, this might be able to help):

local function CharacterAdded(char)
	print("Character Exists")
	char:WaitForChild("Humanoid").Died:Connect(function()
		print(char.Name .. " has died!")
	end)
end


local function PlayerAdded(player)
	player:LoadCharacter()
	--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)

local function CharacterRemoved()
print("You died!")
end

local function PlayerRemoved(player)
	player.CharacterRemoving:Connect(function(CharacterRemoved)

		
		
	end)
end


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

It’s a loop that checks if a player has already been added, aka (v)

1 Like