Jump stamina script breaks in water

guys i have a jump stamina script whenever you jump it reduces the stamina and if you have no stamina you cant jump. but when in terrain water the jump stamina depletes instantly cuz it fires so fast. I dont want to add a time jump cooldown but a stamina based cooldown because i need players to be able to get onto rafts easily

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Gui = script.Parent
local Frame = Gui:WaitForChild("Frame")
local Bar = Frame:WaitForChild("Bar")

Gui.Enabled = true
local MaxStamina = 140
local Stamina = 140

local Swimming = false



local Humanoid = Character.Humanoid

local function RefilStamina()
    local IsJumping = false
    
    task.spawn(function()
        IsJumping = Humanoid.Jumping:Wait()
    end)
    task.wait(3)
    
    while Stamina < MaxStamina and not IsJumping do
        Stamina = Stamina + 5
        
        Bar.Size = UDim2.new(1,0,Stamina/MaxStamina,0)
        if Stamina >= 20 then
            Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
            task.wait(.2)
            if Stamina == MaxStamina then
                Bar.Visible = false
            end
        end
    end
end


Humanoid.Jumping:Connect(function(Active)
    if Active and Humanoid:GetState() ~= Enum.HumanoidStateType.Swimming and Humanoid:GetState() ~= Enum.HumanoidStateType.Seated then
        Bar.Visible = true
        Bar.Size = UDim2.new(1,0,Stamina/MaxStamina,0)
        if Stamina < 20 then
            Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
        end
    else 
        RefilStamina()
    end
end)

So do you not want to reduce the stamina of the player when the player swimming at all? Or you want to reduce the player’s stamina, but not so fast? I want you to clarify that.

i want to reduce the stamina of the player only when jumping not when swimming. but somehow because of how terrain water works, if you jump in the water the jump event fires too fast and it depletes stamina instantly.

1 Like

But didn’t you put

Humanoid:GetState() ~= Enum.HumanoidStateType.Swimming

in your if statement?
Is it not working?

1 Like