Help with how to make a progress bar

Hello, I want to make a tool where if the player holds down left click on the mouse, a progress bar should show up and slowly fill up. If the player were to let go of left click, the progress should pause and resume from where it left off until the player decides to continue holding left click again. And also prevent it from going beyond the maximum length of the progress bar GUI.

Here is my script so far:

local players = game:GetService("Players")
local player = game.Players.LocalPlayer
local tool = script.Parent
local mouse = player:GetMouse()
local handle = tool:WaitForChild("Handle")
local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Handle.Sweep)
local MAXBar = 100
local BarProgress = 1
local bar = players.LocalPlayer.PlayerGui.SweepBar.Background.Bar
local held = false

local currentProgress = player.PlayerGui:WaitForChild("SweepBar").Progress




function updateGui()
	player.PlayerGui.SweepBar.Background.Bar:TweenSize(UDim2.new(0,(currentProgress.Value/MAXBar * 246), 1, 0), "Out", "Linear", 1)
	currentProgress:GetPropertyChangedSignal("Value"):Connect(function()
		player.PlayerGui.SweepBar.Background.Bar:TweenSize(UDim2.new(0,(currentProgress.Value/MAXBar * 246), 1, 0), "Out", "Linear", 1)
	end)
	
end

tool.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		held = true
		animation:Play()
		players.LocalPlayer.PlayerGui.SweepBar.Enabled = true
		
	end)
	mouse.Button1Up:Connect(function()
		held = false
	end)
end)

tool.Activated:Connect(function()
	tool.Handle["Broom Sound Effect"]:Play()
	if currentProgress.Value < MAXBar then
		while held == true do
			updateGui()
			currentProgress.Value = currentProgress.Value + 0.1
			wait(0.1)
		end
	end
	if currentProgress.Value >= MAXBar then
		player.PlayerGui:FindFirstChild("SweepBar").TextLabel.Text = "Sweep Complete"
	end
end)

tool.Deactivated:Connect(function()
	tool.Handle["Broom Sound Effect"]:Stop()
	animation:Stop()
	players.LocalPlayer.PlayerGui.SweepBar.Enabled = false
end)

Currently with this code, the number value inside of the player’s GUI folder stays at 0 and refuses to go up. Unless I were to change the “while held == true do” part to " while true do" it seems to work, but the progress goes up extremely fast, and continues to go up infinitely beyond the progress bar’s UI. Any help or any other questions please leave a comment below. Thank you!

I think you should put the tool.activated function inside the tool.equipped one, have you tried that?

there’s no reason to make a variable to see if you are holding it, also print that variable so we can see what’s the problem

Yea those are variables left over from me trying to figure out how to get this working. Decided to move them under the tool.Activated function, and changed the “while held == true do” to “while held and currentProgress.Value <= MAXBar do” and it seems to be working. However the GUI progress bar isn’t filling up completely… any idea why?

Are you using scale or offset?

I’m using this:

function updateGui()
	player.PlayerGui.SweepBar.Background.Bar:TweenSize(UDim2.new(0,(currentProgress.Value/MAXBar * 246), 1, 0), "Out", "Linear", 1)
	currentProgress:GetPropertyChangedSignal("Value"):Connect(function()
		player.PlayerGui.SweepBar.Background.Bar:TweenSize(UDim2.new(0,(currentProgress.Value/MAXBar * 246), 1, 0), "Out", "Linear", 1)
	end)
	
end

I’d really appreciate help in this area as I’ve never messed around with GUI progress bars before until now. This is just a solution I found on YouTube but I didn’t really understand the information as much since it wasn’t really explained well.

Uhh i mean, you’re using scale for X and offset for Y, i’m not a gui designer so they are going to kill me but, try using scale for all the guis you make because scale works for all the resolutions and offset is weird

maybe this helps you to understand offset and scale

Thank you very much, everything seems to be working now as I intended :slightly_smiling_face:

1 Like

No problem :slight_smile: Remember, use scale in guis!!

1 Like