PlayerAdded not firing. What could be the issue?

I followed a duel system tutorial not so long ago but PlayerAdded didn’t work in the original script.

So I decide to put a few checks incase the Player and their Character got added before the function fired. It fixed the issue and it works but only in Studio and not in game. Maybe the “if char then” could’ve yielded it. But I really cannot figure out what I could do to fix it.

local function CharacterAdded(char)
	print("This block works too")
	local Humanoid = char:FindFirstChildOfClass("Humanoid")
	if not Humanoid then return end

	Humanoid.Died:Connect(function()
		print("Haha dummy died")
		local NewPlayer = Players:GetPlayerFromCharacter(char)
		if NewPlayer:GetAttribute("CurrentMatch") == '' then return end
		local CreatorTag = Humanoid:WaitForChild("creator")
		if not CreatorTag then return end

		local PlayerThatKilled = CreatorTag.Value -- Players:FindFirstChild()

		if not PlayerThatKilled then return end

		if not Players:FindFirstChild(PlayerThatKilled.Name) then return end; if PlayerThatKilled:GetAttribute("CurrentMatch") == '' then return end

		local MatchTable = Manager.GetTable(PlayerThatKilled:GetAttribute("CurrentMatch"))
		if not MatchTable then return end

		local Result = Match.AwardPoints(MatchTable, PlayerThatKilled)
		MatchTable.LastPoint = PlayerThatKilled
		UpdatePoints(MatchTable)

		if Result ~= "No Winner" then
			Match.SetWinner(MatchTable, Result)
		end

		Match.NewPoint(MatchTable)

	end)
end

local function PlayerAdded(NewPlayer)
	print("this block works")
	ReplicatedStorage.RemoteEvents.UpdatePlayers:FireAllClients(NewPlayer, "Joined")
 
	Data.Init(NewPlayer)
	Data.Get(NewPlayer)
	 
	NewPlayer.CharacterAdded:Connect(CharacterAdded)

	 
	local char = NewPlayer.Character or NewPlayer.CharacterAdded:Wait() 
	if char then
		CharacterAdded(char)
	end
end

 
game.Players.PlayerAdded:Connect(PlayerAdded)

 
for i,v in pairs(game.Players:GetPlayers()) do
	spawn(function() PlayerAdded(v) end)
end

You added print statements, what is the last print statement that actually prints something?

What do you mean exactly? I am a little bit confused

Out of all the "Print()"s which print to the output and in what order?

What do these two functions do? Do they yield?

If you are doing something like DataStore:GetAsync() or wait() before you are listening for NewPlayer.CharacterAdded:Connect() the connection might start listening after the character gets added.

Edit:
My fault I have not seen the local char = NewPlayer.Character; below those lines of code. So that obviously isn’t the issue.

All of them print in Studio and in this order
“This block works”
“This block works too”

Then if player died it goes

“Haha Dummy died”
“This block works too”

But in game none of those print, it isn’t a local script btw forgot to mention

Yeah Data.Get has DataStore:GetAsync()
I tried temporarily removing the two which didn’t work, then I tried to call them after CharacterAdded:Connect() which also did not fix the issue.

I think you’re missing the “NewPlayer” parameter for the PlayerAdded function when it is called later in the script:

Replace this code with:

game.Players.PlayerAdded:Connect(PlayerAdded(player))

I think that the player parameter requested in the function is missing when you call the function on this line, so you need to define player with the instance returned via PlayerAdded (defined by player). This will be the case if the NewPlayer parameter represents an added player instance within game.Players

1 Like

If all the solutions wont work I guess you can just resort to replacing the code to this:

game.Players.PlayerAdded:Connect(function(Plr)
PlayerAdded(Plr)
end)

Cheers!

1 Like

I think this is a valid point! Although I did try doing that but I still have the same issue, works in studio but not in game (I did publish the game afterwards).

Were you using the original script (unchanged) or a new script? Try it on the original script.

That’s probably the best option to try out since the function is only called once (for PlayerAdded). Defining the function earlier in the script is unnecessary if you only use it once.

I was using the script I posted, the original was something like

-player added
(
-character added
(
-humanoid died
)
)

Which doesnt work at all since when it fires the player has already fully loaded in. Thats why I made a seprate function which I call in that for in pairs cycle

1 Like