Hello, first, congrats on learning how to script it’s a long journey but take your time.
Now onto the code fix.
You have a few things that seem to be due to a lack of understanding:
- Object Values
When dealing with object values and creating a variable you don’t include .Value in the variable. This is because you need to find the value when the variable is called, not defined aka do
local IsSprinting = Checks:WaitForChild("IsSprinting")
not
local IsSprinting = Checks:WaitForChild("IsSprinting").Value - Debounces
This is how you make sure your functions aren’t stepping on each other’s toes. So simply have a variable (in this caselocal refilling) that lets you know this functions is already running - RunService
You don’t seem to know whats the difference is between renderstepped, stepped, and heartbeat. So ill just attach a link that should help with that. - Not vs == false
You use not (variable) a lot in your code and I would be careful as you should only use it when you know what the variable should be or when checking if something exists. If you are only having a function run with something is false just do == false, It also helps with readability!
Anyways here’s your fixed code!
repeat task.wait() until game:GetService("Players"):GetPlayerFromCharacter(script.Parent) ~= nil
--// Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
--// Client
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Player = Players:GetPlayerFromCharacter(Character)
--// Variables
local Refilling = false
local Pause = false
local SprintTick = 0
--// Status
local Status = Character:WaitForChild("Status")
local Checks = Status:WaitForChild("Checks")
local Stamina = Status:WaitForChild("Stamina")
local IsSprinting =Checks:WaitForChild("IsSprinting").Value
--// Functions
local function RefillStam()
-- Everytime I stop sprinting I would like to add a delay here before regenerating stamina ( task.wait(3) doesn't seem to work )
if Refilling then return end
Refilling = true
if Pause then
task.wait(3)
Pause = false
end
if (Stamina.Value < Stamina.MaxValue) and not IsSprinting and _G.CanUse(Character, "StamRegen") then
local CalcStam = (Stamina.MaxValue/100) * 1.25
Stamina.Value = Stamina.Value + CalcStam
task.wait(0.25)
end
Refilling = false
end
RunService.Heartbeat:Connect(function()
if Humanoid.Health == 0 or not Character.Parent then
return
end
if tick() - SprintTick > 0.25 then
SprintTick = tick()
if Checks:WaitForChild("IsSprinting").Value then
Stamina.Value -= 5
Pause = true
end
end
RefillStam()
end)
Stamina Fix.lua (1.5 KB)