I have a stamina script where there’s a regenerate stamina function, the problem is after sprinting and jumping, it would run the same function again which i don’t want that
preview code
local function RegenStamina()
wait(regendelaytime)
Module.Regenerate = true
end
----jumping----
Humanoid.Jumping:Connect(function(state)
if state == true then
--print("jumped")
Module.Regenerate = true
consumeamounthere
else
RegenStamina()
end
end)
-----sprinting----
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.KeyCode == SprintKey and gameProcessed == false and UserInputService:IsKeyDown(Enum.KeyCode.W) then
sprintingstuffhere = true
end
end)
----stop sprinting---
UserInputService.InputEnded:Connect(function(input, gameProcessed)
if input.KeyCode == SprintKey and gameProcessed == false or not UserInputService:IsKeyDown(Enum.KeyCode.W) then
sprintingstuffhere = false
RegenStamina()
end
end)
If I understand correctly, every time the player performs an action, the regenerate function must be called, but you want to avoid it regenerating multiple times. (Google Translate)
Try this
local globalregencheck = 0
local function RegenStamina()
globalregencheck += 1
local regencheck = globalregencheck
wait(regendelaytime)
if regencheck ~= globalregencheck then return end
Module.Regenerate = true
end
local tick = tick
local wait = wait
local function CustomWait(seconds)
local startTime = tick()
while tick() - startTime < seconds do
task.wait(0.1)
end
end
local function ResetWait()
startTime = tick() -- Reset the start time
end
CustomWait(2)
ResetWait()
It is printing because the function is being called but not executed to completion. Basically what the function does is store the moment in which it is being called and after the “regendelay” the function will check if it was called again, if so, the regeneration will not happen and will let the last call do the regeneration. (Google Translate)