Lerping GUI bar

I’ve wanted to make a bar that fills up when you hold your mouse button and if you arent holding it slowly goes down, and when the bar is full it triggers an event where you throw a soap bar, since I never did lerping i’m having some problems, anyone know how to fix it and maybe any tips??

local bar = script.BarGUI
local char = game.Players.LocalPlayer.Character
local mouse = game.Players.LocalPlayer:GetMouse()
bar.Parent = char:WaitForChild("HumanoidRootPart")
bar = bar.Bg.Bar
local pressing = false
local alpha = 0
mouse.Button1Down:Connect(function()
	pressing = true
	print("Holding")
end)
mouse.Button1Up:Connect(function()
	pressing = false
	print("Not Holding")
end)

while task.wait() do
	print(alpha)
	if alpha >= 0.067 then
		print("Can throw")
		bar.Size = bar.Size:Lerp(UDim2.fromScale(1,1),1)
		continue
	elseif alpha < 0 then
		alpha = 0
		bar.Size = bar.Size:Lerp(UDim2.fromScale(1,0),1)
		continue
	end
	if pressing then
		alpha += 0.0007
		bar.Size = bar.Size:Lerp(UDim2.fromScale(1,1),alpha)
	else
		alpha -= 0.001
		bar.Size = bar.Size:Lerp(UDim2.fromScale(1,1),alpha)
	end
end

Don’t do this it isn’t consistent between devices and play sessions. Do like RunService.RenderStepped and then get the deltaTime and multiply it by some number

Wdym with that like ik what run setvice is but why the delta time

Also that would make it locked to frame so more frame = faster charge up

task.wait() (when no argument is provided) is already frame rate dependent I believe (they changed it to this behaviour a few months ago).

The point of using deltaTime is that you multiply the changed alpha values by delta time (times some constant), such that the rate will be the same for each frame rate.

The calculation involving delta time would be something likeVALUE * DELTATIME, where value is the amount that it changes per second, and deltatime is the value from RenderStepped.

(If you don’t want to use RenderStepped while still making use of delta time, you can utilise the return value of task.wait(), as it is also effectively delta time)

(Ill examine the actual issue in your post rq)

1 Like

yea but in the end i js made this

I would still recommend making use of delta time / the return value of task.wait(), otherwise different framerates will have different ‘charge’ rates

e.g.

while true do
	local dt = task.wait()
	
	if pressing then
		alpha += 0.01 / 60 * dt
	else
		alpha -= 0.01 / 60 * dt
	end
	alpha = math.clamp(alpha, 0, 1)
	bar.Size = UDim2.fromScale(1, alpha)
	if alpha == 1 then
		print("yes")
	end
end

Also, did your change resolve the issue you were having, or do you still need assistance with it?

1 Like

nah thanks it works fine now
heres the final code

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