Cooldown isn't working properly

this is probably the first code that I wrote without any tutorials or anything and the cooldown is sort of working but I can spam click “x” but it would start spamming the power and I don’t want that to happen since that would be too powerful.

can someone tell me where I messed up?
the code:

local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")
local character = player.Character
local Sprint = character.SprintScript
local Fly = character.Wanda.Flight
local burstAnim = 					 
character:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("BurstAnim"))h
local targets = workspace:GetChildren("Humanoid")
local damage = 50
local canBurst = true
local cooldown = 15

UIS.InputBegan:Connect(function(input,gameprocessed)
	if gameprocessed then return end
	if input.KeyCode == Enum.KeyCode.X then
		if not canBurst then return end
	
		canBurst = false
	
		burstAnim:Play()	
	
	character.Humanoid.WalkSpeed = 0
	Sprint.Disabled = true
	Fly.Disabled = true
	
	local burst = Instance.new("Part")
	burst.Anchored = true
	burst.CanCollide = false
	burst.Parent = workspace
	burst.Position = player.Character.HumanoidRootPart.Position
	burst.Shape = Enum.PartType.Ball
	burst.Material = Enum.Material.ForceField
	burst.Color = Color3.fromRGB(120, 0, 0)
	burst.CastShadow = false
	burst.Name = "Burst"
	
	local tweenInfo = TweenInfo.new(4,Enum.EasingStyle.Circular,Enum.EasingDirection.Out,0,false,0)
	local sizegoal = {Size = Vector3.new(50, 50, 50)}
	local tween = TS:Create(burst, tweenInfo, sizegoal)
	
	tween:Play()
	
	wait(3.6)
	burst.Transparency = 0.5
	wait(0.1)
	burst.Transparency = 0.8
	wait(0.1)
	burst.Transparency = 1
	
	burst:Destroy()
	
	character.Humanoid.WalkSpeed = 16
	Sprint.Disabled = false
	Fly.Disabled = false

	end

	wait(cooldown)
	canBurst = true
end)

I think it is because you are checking if canBurst is nil rather than false.

if not canBurst then return end

I would try

if canBurst == false then return end

or

if canBurst == true then
1 Like

Fun fact, not X will work if X is either nil or false!

Try Debugging | Roblox Creator Documentation to step through the code line by line and see if everything is what you expect.

Instead of

if input.KeyCode == Enum.KeyCode.X then
	if not canBurst then return end
	
	canBurst = false

    --the rest of your code
end

wait(cooldown)
canBurst = true

try

if input.KeyCode == Enum.KeyCode.X then
	if not canBurst then return end
	
	canBurst = false

    --the rest of your code

    wait(cooldown)
    canBurst = true
end
1 Like

this worked! thank you so much

1 Like