Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

why my fireball does not have a cooldown?

i try to make a cooldown for my fireball, when i try to spamming it, it does not have a cooldown. Help me please

this is the fireball script

local Character = Player.Character

	
    Fireball.Position = Character.RightHand.Position
    Fireball.Parent = workspace
    local velocity = Instance.new("BodyVelocity", Fireball)
    velocity.Velocity = CFrame.new(Fireball.Position, Mouse).LookVector * 50
	 	Animation:Play()
	debounce = true
	

	    Fireball.Touched:Connect(function(Touched)
		
		        if Touched:IsDescendantOf(Character) then return end
		Fireball:Destroy()

       
       
        if Touched.Parent:FindFirstChild("Humanoid") then
			            Touched.Parent:FindFirstChild("Humanoid"):TakeDamage(10)
		        end
		 wait(3)
    debounce = false
    end)
end)

and this is the fireball activation script

local UIS = game:GetService("UserInputService")
local mouse = game.Players.LocalPlayer:GetMouse()
local debounce = false

UIS.InputBegan:Connect(function(key, processed)
	if processed or debounce then return end
	   
    if key.KeyCode == Enum.KeyCode.Q then
		debounce = true
	    workspace.FireballScript.RemoteEvent:FireServer(mouse.Hit.Position)
	end

    wait(3)
    debounce = false
end)

there is nothing error with the code

2 answers

The main problem is inside of this part of the code:

local UIS = game:GetService("UserInputService")
local mouse = game.Players.LocalPlayer:GetMouse()
local debounce = false
 
UIS.InputBegan:Connect(function(key, processed)
    if processed or debounce then return end
 
    if key.KeyCode == Enum.KeyCode.Q then
        debounce = true
        workspace.FireballScript.RemoteEvent:FireServer(mouse.Hit.Position)
    end
 
    wait(3)
    debounce = false
end)

To add a cooldown, you need to check if the cooldown is currently in place before shooting the fireball by using an if statement by checking if debounce is true before firing the event. You also don't really need to check if processed. Here is the edited code:

local UIS = game:GetService("UserInputService")
local mouse = game.Players.LocalPlayer:GetMouse()
local debounce = false

UIS.InputBegan:Connect(function(key, processed)
--check if debounce is false
if debounce == false then
	if key.KeyCode == Enum.KeyCode.Q then
		debounce = true
		--here
		wait(3)
		debounce = false
		workspace.FireballScript.RemoteEvent:FireServer(mouse.Hit.Position)
	end
end)

Also, if the fireball is delaying by 3 seconds and you do not want it to delay, replace "--here" with "workspace.FireballScript.RemoteEvent:FireServer(mouse.Hit.Position)" and delete the old "workspace.FireballScript.RemoteEvent:FireServer(mouse.Hit.Position)".

2 comments

0

where i should put the second end cause there is 2 if statement

0

roblox lua (unlike other languges) really does not care about where you indent so just add another one on the line that the end) is on

Here is what i think it will fix your problem

local UIS = game:GetService("UserInputService")
local mouse = game.Players.LocalPlayer:GetMouse()
local debounce = false

UIS.InputBegan:Connect(function(key, processed)
 if debounce == false then
    if processed or debounce then return end

    if key.KeyCode == Enum.KeyCode.Q then
        debounce = true
        workspace.FireballScript.RemoteEvent:FireServer(mouse.Hit.Position)
    end

    wait(3)
    debounce = false
end
end)

Probably you forget to detect if debounce was on true so it dont keep spamming.

This discussion is preserved as it was. New answers, comments, and votes are closed.