Bar Script Not Working

Let’s get straight to the problem, I made a bar system that when the key Z is pressed the bar will go down and when it has an specific size it will have like a re-fill effect, soo It would be easily exploited and cheated and it was kind of buggy and I tried to make a cooldown for it, it works when I press Z but the bar doesn’t go back up how it used to, here is the code and its a localscript.


--// CONFIGURATIONS \\--
local x = game.ReplicatedStorage.Classes.TestClass.Cooldown.Value
local xConsume = 4
local xReload = 6
local Cooldown = false
debounce = true

--// VARIABLES \\--
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local RunService = game:GetService("RunService")

local MainBar = script.Parent.MainBar
local Bar = MainBar.Bar
local Text = MainBar.Text

--// MAIN CODE \\--
if debounce then
	UserInputService.InputBegan:Connect(function(Input)
		if not Cooldown then
		  if Input.KeyCode == Enum.KeyCode.Z then  
				MainBar.Bar:TweenSize(UDim2.new(0, 0,0, 53), "In", "Quad", x - 12)
				wait(xConsume)
				Cooldown = true
				debounce = false
			end
		end
	end)
end

if debounce == false then
	RunService.Heartbeat:Connect(function()
		if Cooldown then
			 if MainBar.Bar.Size == UDim2.new(0, 0,0, 53) then
				wait(0.6)
				MainBar.Bar:TweenSize(UDim2.new(0, 205,0, 53), "In", "Quad", x)
				wait(xReload)
				Cooldown = false
				debounce = true
			end
		end
	end)
end

Maybe you should change:

to:

instead.

2 Likes

Thanks for the response, but it still doesn’t seem to work.

I think you put the heartbeat function in the wrong line.
Try doing this:

1 Like

I don’t understand why you are using heartbeat here. You could simply just refill the bar in the InputBegan function. Also the debounce check should be in the InputBegan function like this, if not Cooldown or debounce then.

Currently setting debounce to false won’t do anything since you check if it is true before you run the UIS function.

2 Likes