Lag Affects Regen rate?

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)

Mobile / Any smooth device :

PC / Any Slow Device (Probably my low end pc)

1 Like

You could look into .Heartbeat instead. It registers every frame so it’s specific to frame rate of device

2 Likes

And by using that you can use Delta as you mentioned maybe using

1 Like

Change character:SetAttribute("Energy", character:GetAttribute("Energy") - 0.15)
to character:SetAttribute("Energy", character:GetAttribute("Energy") - 0.15 * delta) (divide or multiply delta by whatever speed u want)

1 Like

i tried doing that (multiplying), but it bugged weirdly , and in the end i just did this :

character:SetAttribute("Energy", character:GetAttribute("Energy") - delta/0.15) -- 0.15
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

1 Like

I see, so in that case i Should do regenRate*delta ? (maybe i got it wrong)

yeah assuming regenRate is the amount you want per second

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.