#Stamina Scripting failing in existing help

1 messages · Page 1 of 1 (latest)

tribal finch
#
local RunService = game:GetService("RunService")

local MaxStamina = script.Parent:GetAttribute("MaxStamina")
local CurrentStamina = script.Parent:GetAttribute("CurrentStamina")

local RunSpeed = script.Parent:GetAttribute("RunSpeed")
local WalkSpeed = script.Parent:GetAttribute("WalkSpeed")

local Exhausted = false
local Running = false

local CanRunValue = 25
local RecoverRate = 1
local DrainRate = 25

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local UI = script.Parent
local Bar = UI.HolderFrame.SprintBackground.Bar

local function InputBegan(Input, GameProcess)
    if Exhausted then return end
    if Input.KeyCode == Enum.KeyCode.LeftControl and not GameProcess then
        Humanoid.WalkSpeed = RunSpeed
        Running = true
    end
end

local function InputEnded(Input, GameProcess)
    if Input.KeyCode == Enum.KeyCode.LeftControl and not GameProcess then
        Humanoid.WalkSpeed = WalkSpeed
        Running = false
    end
end

local function UpdateStaminaUI()
    Bar.Size = UDim2.new(math.clamp(CurrentStamina/MaxStamina, 0, 0.996), 0, 0.65,0)
end

RunService.Heartbeat:Connect(function(DeltaTime)
    if Running and Humanoid.MoveDirection.Magnitude > 0 then
        CurrentStamina = math.min(100, CurrentStamina - DrainRate * DeltaTime)
        UpdateStaminaUI()
        if CurrentStamina <= CanRunValue or CurrentStamina <= 0 then
            Exhausted = true
        end
    elseif not Running then
        CurrentStamina = math.min(100, CurrentStamina + RecoverRate * DeltaTime)
        UpdateStaminaUI()
        if CurrentStamina >= CanRunValue then
            Exhausted = false
        end
    end
end)

UserInputService.InputBegan:Connect(InputBegan)
UserInputService.InputEnded:Connect(InputEnded)
#

The mains errors of this script are the fact that the player doesnt stop sprinting when the stamina ends, just when the player stops pressing left ctrl

#

and the stamina doesnt regen anymore and idk why

final prawn
#

first problem is that you still can run after stamina runs out?

#

did i understand well

#
local RunService = game:GetService("RunService")

local MaxStamina = script.Parent:GetAttribute("MaxStamina")
local CurrentStamina = script.Parent:GetAttribute("CurrentStamina")

local RunSpeed = script.Parent:GetAttribute("RunSpeed")
local WalkSpeed = script.Parent:GetAttribute("WalkSpeed")

local Exhausted = false
local Running = false

local CanRunValue = 25
local RecoverRate = 1
local DrainRate = 25

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local UI = script.Parent
local Bar = UI.HolderFrame.SprintBackground.Bar

local function InputBegan(Input, GameProcess)
    if Exhausted then return end
    if Input.KeyCode == Enum.KeyCode.LeftControl and not GameProcess then
        Humanoid.WalkSpeed = RunSpeed
        Running = true
    end
end

local function InputEnded(Input, GameProcess)
    if Input.KeyCode == Enum.KeyCode.LeftControl and not GameProcess then
        Humanoid.WalkSpeed = WalkSpeed
        Running = false
    end
end

local function UpdateStaminaUI()
    Bar.Size = UDim2.new(math.clamp(CurrentStamina/MaxStamina, 0, 0.996), 0, 0.65,0)
end

RunService.Heartbeat:Connect(function(DeltaTime)
    if Running and Humanoid.MoveDirection.Magnitude > 0 then
        CurrentStamina = math.min(100, CurrentStamina - DrainRate * DeltaTime)
        UpdateStaminaUI()
        if CurrentStamina <= CanRunValue or CurrentStamina <= 0 then
            Exhausted = true
            Humanoid.WalkSpeed = WalkSpeed
            Running = false
        end
    elseif not Running then
        CurrentStamina = math.min(100, CurrentStamina + RecoverRate * DeltaTime)
        UpdateStaminaUI()
        if CurrentStamina >= CanRunValue then
            Exhausted = false
        end
    end
end)

UserInputService.InputBegan:Connect(InputBegan)
UserInputService.InputEnded:Connect(InputEnded)
#

also i do not think that this script will still work after the character dies

tribal finch
#

srry if i writed it wrong

final prawn
#

nah

#

just to be clear

tribal finch
#

k

#

also

#

the main errors i alr fixxed

final prawn
#

oh

tribal finch
#

but i found another one right here

final prawn
#

which one?

#

mind if i can help

tribal finch
#

yeah u can help

final prawn
#

which error?

tribal finch
#

by some reason the stamina doesnt regen when i press ctrl while i stand

final prawn
#

wait

tribal finch
#

i will send a video give me a moment

final prawn
#

do you mean

tribal finch
#

like there is a line that prevents the player lose stamina while standing still

final prawn
tribal finch
final prawn
#

so

#

you want to make it

#

regen the stamina if player is standing still?

tribal finch
#

yeah

#

it already do

final prawn
#

and whether they pressed ctrl or not

tribal finch
#

yeah

final prawn
#

i see

#

can you send the fixed code here?

tribal finch
#

like the player just lose stamina while walking

final prawn
#

i mean the current script

tribal finch
#

you can see txt?

#

like .txt

final prawn
#

no

#

sorry i cant

tribal finch
#

oh

final prawn
#

well but happy news

#

i found a solution

tribal finch
#

ill send just the runservice part

tribal finch
final prawn
#

you see this
elseif not running then

#

you just change it to else only

#

elseif blabla -> else

#

dont touch anything else

#

only this

tribal finch
#

wait what

#

like elseif not running else not walking part

final prawn
#

what

#

I mean

#

did you code it?

#

i’ll make you understand the code

tribal finch
#

wait let me send the script

final prawn
#

if you coded it

#

u dont have to

tribal finch
#

yea i coded it

#
    if Running and Humanoid.MoveDirection.Magnitude > 0 then
        CurrentStamina = math.max(0, CurrentStamina - DrainRate * DeltaTime)
        UpdateStaminaUI()
        if CurrentStamina <= CanRunValue or CurrentStamina <= 0 then
            Exhausted = true
        end
        if CurrentStamina <= 0 then
            Humanoid.WalkSpeed = WalkSpeed
            Running = false
        end
    elseif not Running then
        CurrentStamina = math.min(100, CurrentStamina + RecoverRate * DeltaTime)
        UpdateStaminaUI()
        if CurrentStamina >= CanRunValue then
            Exhausted = false
        end
    end
end)```
final prawn
tribal finch
#

here it is

final prawn
#

you find this line

#

and

#

elseif not Running then
to
else

#

done

tribal finch
#

like this?

#

oh it worked

final prawn
#

elyeah

tribal finch
#

wow

#

you are a genius

#

thank you

final prawn
#

haha thanks

#

glad to help you

tribal finch
#

let me just see if everything is working

#

and ill close the chat

final prawn
#

okay

tribal finch
#

yeah its working

#

everything perfect

#

thanks man

final prawn
#

haha

#

have a great day bro