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)