Local script stops working when a player die

this script is in StarterPlayerScripts and it stop printing/working when a player die


-- { VARIABLES } --
local Player = game.Players.LocalPlayer
local Character :Model = Player.Character or Player.CharacterAdded:Wait()
local Humanoid :Humanoid = Character:FindFirstChild("Humanoid") or Character:FindFirstChild("Humanoid"):Wait()
local Alive = false
-- { FUNCTIONS } --


-- { CORE GAME CODE } --

Humanoid.StateChanged:Connect(function(_,after)
	if after == Enum.HumanoidStateType.Dead then
		Alive = false
		task.wait(2)
		print(Alive) -- stops printing when a player die
	else
		Alive = true
		end
		print(Alive) -- stops printing when a player die
	end)

if you donot understand what i mean copy this script and add it in local script and then run the game and move it will output true which is the value of variable { Alive } if you resetted your character it will stop outputting

any help is appreciated and thanks

Hello. I think that I know how it should be fixed. Look at the edited code below:

-- { VARIABLES } --
local Player = game.Players.LocalPlayer
local Character :Model = Player.Character or Player.CharacterAdded:Wait()
local Humanoid :Humanoid = Character:FindFirstChild("Humanoid") or Character:FindFirstChild("Humanoid"):Wait()
local Alive = false
-- { FUNCTIONS } --


-- { CORE GAME CODE } --

Humanoid.StateChanged:Connect(function(_,after)
	if after == Enum.HumanoidStateType.Dead then
		Alive = false
		task.wait(2)
		print(Alive) -- stops printing when a player die
	else
		Alive = true
	end
	print(Alive) -- stops printing when a player die
end)

-- To fix the script, we need to
-- re-declare character and humanoid
-- every time when player dies
Player.CharacterAdded:Connect(function(character)
	Character = character
	Humanoid = Character:FindFirstChild("Humanoid") or Character:FindFirstChild("Humanoid"):Wait()
end)
1 Like

thanks but when i use it the same bug happen it works well until i die and then it stops printing true whenever i move/jump can you try this code in roblox studio and reset your character and then move?
i want to know if this proplem happens to me only and thanks

you can try putting the script in startercharacterscripts instead so that it reruns every time the character is loaded

2 Likes

Hi, i tested this code and it works here:

-- { VARIABLES } --
local Player = game.Players.LocalPlayer
local Alive = false

Player.CharacterAdded:Connect(function()

	local Character :Model = Player.Character
	local Humanoid :Humanoid = Character:FindFirstChild("Humanoid") or Character:FindFirstChild("Humanoid"):Wait()
	local Alive = false

	Humanoid.StateChanged:Connect(function(_,after)
		if after == Enum.HumanoidStateType.Dead then
			Alive = false
			task.wait(2)
			print(Alive)
		else
			Alive = true
		end
		print(Alive) 
	end)
end)
2 Likes

that works thanks i didnot know that the scripts in starter character reset each time character is loaded

1 Like

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