Hi, i am doing a movement system, and currently, i have a problem with the regen rate of my stamina, Lag or smoothness affects the regen rate, and is a lot noticeable, is there any way to fix this ? , i was thinking on using delta, but i don’t know how could exactly use it right here
local function update(delta)
if humanoid.MoveDirection.Magnitude > 0 and running and not crouching and character:GetAttribute("Energy") > 0 then
humanoid.HipHeight = 0
humanoid.WalkSpeed = 25
_G.ViewbobSpeed = 14
regenTime = time()
character:SetAttribute("Energy", character:GetAttribute("Energy") - 0.15)
else
if not crouching then
humanoid.HipHeight = 0
humanoid.WalkSpeed = game.StarterPlayer.CharacterWalkSpeed
_G.ViewbobSpeed = 11
else
humanoid.HipHeight = crouchY
humanoid.WalkSpeed = 12
end
--if stamina < maxStamina then
--if (time() - regenTime) > 5 then
-- stamina += regenRate
--end
--end
end
if humanoid.MoveDirection.Magnitude > 0 then
regenRate = 0.25
else
regenRate = 0.35
end
if character:GetAttribute("Energy") < maxStamina then
if (time() - regenTime) > 5 then
character:SetAttribute("Energy", character:GetAttribute("Energy") + regenRate)
end
end
_G.STAMINA = character:GetAttribute("Energy")
end
task.spawn(function()
runService.RenderStepped:Connect(update)
end)
runService.RenderStepped:Connect(function(delta)
healthBar.Size = healthBar.Size:Lerp(UDim2.fromScale(humanoid.Health / humanoid.MaxHealth,1), delta * 12)
energyBar.Size = energyBar.Size:Lerp(UDim2.fromScale(character:GetAttribute("Energy") / maxStamina,1), delta * 12)
end)
if humanoid.MoveDirection.Magnitude > 0 then
regenRate = delta/0.45 --0.25 -- moving, slower regen
else
regenRate = delta/0.30 -- 0.35 -- not moving, faster regen
end
And it fixed it, But now higher numbers means a low regen rate, and lower numbers means a higher regen rate, I can’t explain why, but this is the temporary solution i have for now, If anyone has a better explanation on this, it would be really appreciated
the constant number should be the rate you want per second, and then you want to multiply it by the deltaTime which is measured in seconds, for example, if I want a regen rate of 1 per second, and a lag spike made a frame take 0.5 seconds, 1*0.5 = 0.5 which is exactly half of the regeneration for exactly half a second. Dividing by deltaTime is not the way you do things