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
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)".
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.