Why doesn't Player.CharacterAdded work correctly?

Hello.
I have created a script that works every time Humanoid sits or gets up from a chair.
Basically it works fine, but when the character dies or is reset, it stops working altogether.
I have found out that I can use “CharacterAdded” by looking in the forum.
I have actually improved the script, but apparently it still does not work after a reset.
What am I doing wrong?

local player = game.Players.LocalPlayer
local event = workspace.Events.RemoteEvent
local Character= player.Character or player.CharacterAdded:Wait()
local humanoid = Character:WaitForChild("Humanoid")

player.CharacterAdded:Connect(function(chara)
	Character = chara
	humanoid = chara:WaitForChild("Humanoid")
end)

humanoid.Seated:Connect(function()
	if humanoid.SeatPart then
		if humanoid.SeatPart.Name == "VehicleSeat" then
			event:FireServer(sit)
			print("sitdown VS")
		else
			event:FireServer(nil)
			print("sitdown")
		end
	else
		event:FireServer(nil)
		print("standup")
	end
end)

This LocalScript is stored in StarterPlayerScript.

scripts that deal with the character should go in StarterCharacterScripts

player.CharacterAdded:Connect(function(chara)
	Character = chara
	humanoid = chara:WaitForChild("Humanoid") -- You realized you needed to update humanoid
end)

humanoid.Seated:Connect(function() -- But you're still using the original humanoid here (it doesn't update)
	if humanoid.SeatPart then
		if humanoid.SeatPart.Name == "VehicleSeat" then
			event:FireServer(sit)
			print("sitdown VS")
		else
			event:FireServer(nil)
			print("sitdown")
		end
	else
		event:FireServer(nil)
		print("standup")
	end
end)

Thank you for your help in resolving this issue speedily!
It worked correctly!
I just found out about StarterCharacterScripts.XD

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