Player.Characteradded does not work

I have this part of the code which is running in a script object that has the RunContext set to Client and it seems like it cannot detect when the character gets added, here is the function i used. It wont even get the the print statement in the beginning.

local player = game:GetService("Players").LocalPlayer
player.CharacterAdded:Connect(function(character)
	print("Character added")
	local Humanoid = character:WaitForChild("Humanoid")
	Humanoid.Died:Connect(function()
		if isSpectating then
			disableSpectateMode()

		end
	end)
	Humanoid.Running:Connect(function(speed)
		if speed > 0  and isSpectating then
			print("Player is running")
			disableSpectateMode()
		end
	end)
end)

Update: I had a variable right above it that had a waitforchild and apparently that caused this to not run before the player initially loaded in, It is resolved now

You are still balancing on a thin race condition with this. You should invoke the callback directly if the player already has a character.

local function OnCharacterAdded(Character: Model)
end

if LocalPlayer.Character then
	OnCharacterAdded(LocalPlayer.Character)
end

LocalPlayer.CharacterAdded:Connect(OnCharacterAdded)
1 Like

OH MY GOD YOU ARE SUCH A GENIUS this really helped thank you, i initially just reorginized my code but this worked way better thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.