How to restart a line of code

I have a script that manages jumping and stamina. It checks for humanoid state being jumping and fires a function. The problem is that it doesn’t work after the character died because it is still checking for the old humanoid !!
Here is the script:

--Jumping--
--variables--
local Player = game.Players.LocalPlayer
local Character = Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local ContextActionService = game:GetService("ContextActionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StaminaEvent = ReplicatedStorage.RemoteEvents.Stamina

local StaminaDisplay = Player.PlayerGui.StaminaGui.Background.StaminaDisplay

local stamina = Player.PlayerGui.StaminaGui.Stamina
local running = false
local jumping = false
local regenerating = false

--stamina regen--
local function regen()
	regenerating = true
	wait(1)
	while stamina.Value < 1000 and not running and not jumping do
		StaminaEvent:FireServer(1)
		StaminaDisplay:TweenSize(UDim2.new (stamina.Value / 1000,0,1,0), "Out", "Linear", 0.1)
		if stamina.Value > 250 then
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping,true)
		end
		if Humanoid.MoveDirection.Magnitude == 0 then
			StaminaEvent:FireServer(3)
			StaminaDisplay:TweenSize(UDim2.new (stamina.Value / 1000,0,1,0), "Out", "Linear", 0.1)
		end
		wait(0.01)
	end
	StaminaDisplay:TweenSize(UDim2.new (stamina.Value / 1000,0,1,0), "Out", "Linear", 0.1)
	regenerating = false
end

--Jumping--
Humanoid.StateChanged:Connect(function(old,new)
	if new == Enum.HumanoidStateType.Jumping then
		jumping = true
		StaminaEvent:FireServer(-250)
		StaminaDisplay:TweenSize(UDim2.new (stamina.Value / 1000,0,1,0), "Out", "Linear", 0.1)
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping,false)
		wait()
		jumping = false
		if stamina.Value > 250 then
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping,true)
		end
		if not regenerating then
			regen()
		end
	end
end)

--running--
local function Sprint(actionName,inputState)
	if actionName == "Sprint" and inputState == Enum.UserInputState.Begin then
		running = true
		while stamina.Value > 0 and running do
			if Humanoid.MoveDirection.Magnitude ~= 0 then
				Humanoid.WalkSpeed = 25
				StaminaEvent:FireServer(-4)
				StaminaDisplay:TweenSize(UDim2.new (stamina.Value / 1000,0,1,0), "Out", "Linear", 0.1)
				if stamina.Value < 251 then
					Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping,false)
				end
				if stamina.Value < 0 then
					running = false
					Humanoid.WalkSpeed = 16
					if not regenerating then
						regen()
					end
				end
			end
			wait(0.01)
		end
		if stamina.Value < 0 then
			running = false
			Humanoid.WalkSpeed = 16
			if not regenerating then
				regen()
			end
		end

	elseif actionName == "Sprint" and inputState == Enum.UserInputState.End then
		running = false
		Humanoid.WalkSpeed = 16
		if not regenerating then
			regen()
		end
	end
end

--CAS binding--
ContextActionService:BindAction("Sprint",Sprint,true,Enum.KeyCode.LeftShift,Enum.KeyCode.ButtonL3)
ContextActionService:SetImage("Sprint","rbxassetid://1488065688")
ContextActionService:SetPosition("Sprint",UDim2.new(1, -90, 0.5, -100))

--Changing Character--
Player.CharacterAdded:Connect(function(character)
	Character = character
	Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping,true)
	Humanoid = Character:WaitForChild("Humanoid")
	running = false
	jumping = false
	StaminaEvent:FireServer(1000)
	StaminaDisplay:TweenSize(UDim2.new (stamina.Value / 1000,0,1,0), "Out", "Linear", 0)
end)

add this in StarterCharacterScripts as scripts in this folder would restart every time player respawns

for a reason i ignore, it wont work there. :man_shrugging:

The script in the original post is now the whole script.

where did you put this script? StarterPlayerScripts?

1 Like

yes it is indeed where i put it

You’ve said that it is still checking for the old Humanoid even when the player has died.

A fix could be to “update” or “refresh” the variable. Simply make a event when the player has respawned and then create the Humanoid variable like this:

Humanoid.Died:Connect(function()
Player.CharacterAdded:Wait() 
Humanoid = Character:WaitForChild("Humanoid")
end)
1 Like

I did this (it is at the end of the script) but it’s like if it doesn’t acknowledge that, it stays stuck on the old one

Oops didn’t notice that. You should try seeing if the function even fires by using a print there.

Also, I believe this has to be switched around? I’m not sure tho.

yeah it fires…

character limit

put the local script in startercharacterscripts and change local character = player.CharacterAdded:Wait() to local character = player.Character

1 Like

Thank you soo much !!!
I have been searching for a solution to this for a few weeks now and it works !!!