Help with tweening a cooldown indicator and global cooldown

I have a basic cooldown indicator, and I’m looking for how I can tween a bar to show the progress made on the cooldown (basically just a bar that scales with the cooldown timer), and I’m also looking for a way to make it so if you use a powerup of one type (such as health), you then can’t use any other of this type, I was thinking about making all of the fire an event which gives a cooldown, but I can think of some issues with that.
Here’s my code.

local rs = game:GetService("ReplicatedStorage")
local ToolEvents = rs:WaitForChild("ToolEvents")
local speedUp = ToolEvents.speedup3
local debris = game:GetService("Debris")
local baseSpeed = 16
local speedMulti = 2
local BoostTime = 20
local debuffMulti = .25

speedUp.OnServerEvent:Connect(function(player, cooldown, debounce)
	local CooldownGUI = player.PlayerGui:WaitForChild("CooldownGUI")
	local character = player.Character
	local tool = character:FindFirstChild("SpeedTool3")
	if character.Humanoid.WalkSpeed <= 0 then
		return 0
	end
	if character.Humanoid.WalkSpeed > 16 then
		return 0
	end
	if debounce then
		return 0
	end
	tool:Destroy()
	character.Humanoid.WalkSpeed = character.Humanoid.WalkSpeed*speedMulti
	wait(BoostTime)
	character.Humanoid.WalkSpeed = baseSpeed
	debounce = true
	local cooldownFrame = rs:WaitForChild("Cooldown")
	cooldownFrame.Parent = CooldownGUI
	cooldownFrame.Text = "Speed+"
	local CDBar = cooldownFrame.CooldownBar
	for i = 0,20,1 do
		wait(1)
		CDBar:TweenSize(
			UDim2.new(i/cooldown,0,1,0),
			Enum.EasingDirection.Out,
			Enum.EasingStyle.Quad,
			false,
			.5
		)
	end
	debounce = false
	cooldownFrame:Destroy()
end)

Any help is appreciated.

1 Like

Is there a reason you’re tweening a GUI server-side in Replicated Storage?

If the intent is to replicate it to players, why not update an IntValue and have clients tween their own GUIs on IntValue.Changed?

EDIT: I noticed you’re putting the GUI in a player then tweening it. It’s much better to just count down in an IntValue that the server updates and then have the client do their own cooldown timer.

No, that was just because the power up effects come from a server script, so I tried to make it fit in there, but it would probably be easier just give the player a cooldown value and check for it in all of my powerup scripts

and also send a remote event back to a client script to indicate cooldown

Yes I agree. I would even go as far as creating an object instead for these powerups if you’re interested. Here’s a basic hierarchy:

Disclaimer: in order to understand this make sure you are familiar and comfortable with OOP!!!

In Powerup Obj (module)

local powerup = {}
powerup.__index = powerup

function powerup.new()
   local newPowerup = setmetatable ({}, powerup)
   newPowerup.Debounce = false
   return newPowerup
end

return powerup

In a mock powerup (Like speed) (module)

local powerupObj = require (path.to.powerupModule)
local speedPower = {}
speedPower.__index = speedPower
setmetatable (speedPower, powerupObj)

function speedPower.new (basePowerup) -- ensure it's linked to the same base you link all other powerups
   local newSpeedPower = setmetatable(basePowerup, speedPower)
   local speedBoost = 10
   return newSpeedPower
end

function speedPower:Boost()
   if self.Debounce = false then -- add this check to all kinds of powerups
      self.Debounce = true
      -- do your speed boost stuff
      self.Debounce = false
   end
end

return speedPower

In main script (script)

   local powerupObj = require (path.to.powerupModule)
   local speedObj = require (path.to.speedModule)
   local newPower = powerupObj.new()
   local newSpeedpower = speedObj.new(newPower)
   -- now you can run speed power and like objects from here
   newSpeedPower:Boost()
   newSpeedPower:Boost() -- if debounce is on it won't run as it checks in the function

ADDED NOTE: A few perks about using OOP instead is that it’s easier debugging, more control over when powerups can be used when they’re tied to a central “Powerup hub” and it’s much, much, much easier to read when you come back to it sometime in the future as it’d be neater and more organized.

Guess I know what I’m doing tonight.