Adding a cooldown to a skill

	if gpe then return end
	if input.KeyCode == Enum.KeyCode.E then
		if mouse.Hit then 
			local data = {client = true, c = character}  
			local load = humanoid:LoadAnimation(anims.Fireball)
			load:Play()  
			local sound = rs.Attacks.Fire.Fx.Circle.fireball
			sound.Playing = true
			debris:AddItem(load, load.Length)  
			remote:FireServer("FireCharge", data)  
			fireCharge(data)
			data.endpoint = mouse.Hit.Position      
			remote:FireServer("Fireball", data)    
			fireball(data) 
		end
	end
end)```

Made a fireball skill but i have issues adding a cooldown to it. I tried debounce but im not sure where to put it.

You can create a boolean outside the fireball function indicating that the player can shoot fireball or not. Then, after you shot a fireball, you can spawn a function that will reset the boolean value after x seconds.

local canShootFireball = true

local function fireball()
       if canShootFireball then
             canShootFireball = false

             — Shoot some fireballs

             spawn(function()
                   task.wait(3)
                   canShootFireball = true
             end)
       end
end

Fire balls arent shooting after adding your code for some reason

    local function fireBall()
	if canShootFireball then
		canShootFireball = false
		uis.InputBegan:Connect(function(input, gpe)
			if gpe then return end
			if input.KeyCode == Enum.KeyCode.E then
				if mouse.Hit then 
					local data = {client = true, c = character}  
					local load = humanoid:LoadAnimation(anims.Fireball)
					load:Play()  
					local sound = rs.Attacks.Fire.Fx.Circle.fireball
					sound.Playing = true
					debris:AddItem(load, load.Length)  
					remote:FireServer("FireCharge", data)  
					fireCharge(data)
					data.endpoint = mouse.Hit.Position      
					remote:FireServer("Fireball", data)    
					fireball(data)
				end
			end
		end)
		spawn(function()
			task.wait(3)
			canShootFireball = true
		end)
	end
end```

What are the errors? Can you provide me an image?


no errors

the code that you sent creating an event listener when you hit E key rather than firing the function. Place your inputBegan outside the function and call the fireball function if it detects input like this.

local function fireball()
        — Shoot fireball
end

uis.InputBegan:Connect(fireball)

now the fireballs just freeze in the air without errors too

Alright, before things get more complicated please revert you code back to how it originally was, and paste the whole fireball code in here. Sorry, I didn’t realize that you used the inputBegan when you created this post, that fraction of code was missing.

local rs = game:GetService("ReplicatedStorage")
local debris = game:GetService("Debris")
local ts = game:GetService("TweenService")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local character = script.Parent
local humanoid = character.Humanoid
local hrp = character.HumanoidRootPart

local remote = rs.Remotes.Attacks.FireRemote
local resources = rs.Attacks.Fire
local anims = resources.Animations
local fx = resources.Fx

local camera = workspace.CurrentCamera
local CameraShaker = require(rs.Modules.CameraShaker)

local camShake = CameraShaker.new(Enum.RenderPriority.Camera.Value, function(shakeCFrame)
	camera.CFrame = camera.CFrame * shakeCFrame
end)
camShake:Start() 

local function raycast(startPosition, endPosition, filter, length, obj) 
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Blacklist
	params.IgnoreWater = true	
	params.FilterDescendantsInstances = filter
	
	local result = workspace:Raycast(obj.Position, (endPosition - startPosition).Unit * length, params)
	if result then
		obj.Position = result.Position
		return false
	else
		obj.Position = (endPosition - startPosition).Unit * length + startPosition
		return true
	end
end

local function fireCharge(data) 
	local c = data.c
	local h = c.Humanoid
	local root = c.HumanoidRootPart
	local fireball = fx.Fireball:Clone() 
	fireball.Parent = workspace.Fx 
	
	local handfire = fireball.Attach.Fire:Clone() 
	handfire.Parent = c["Left Arm"].LeftGripAttachment
	handfire.Enabled = true
	
    h.WalkSpeed = 0
	h.JumpHeight = 0
	
	local floorfire = fx.Fireball:Clone() 
	floorfire.CFrame = root.CFrame * CFrame.new(0,-3,0)
	floorfire.RingAttachment.Orientation = Vector3.new(0,0,0)
	floorfire.RingAttachment.Ring.Enabled = true
	floorfire.Parent = workspace.Fx
	
	for i = 1, 2 do 
		local circleMesh = fx.Circle:Clone()
		circleMesh.Parent = workspace.Fx
		circleMesh.CFrame = c["Left Arm"].CFrame * CFrame.Angles(math.rad(math.random(0,180)),math.rad(math.random(0,180)),math.rad(math.random(0,180)))
		debris:AddItem(circleMesh, .5)
		ts:Create(circleMesh,TweenInfo.new(.5), {Transparency = 1, Size = Vector3.new(0,0,0)}):Play()
		wait(.1)
	end
	
	fireball.CFrame = c.HumanoidRootPart.CFrame * CFrame.new(1.5,0, -5)
	handfire.Enabled = false
	handfire:Destroy()
	
	local ring = fireball:Clone()  
	ring.Parent = workspace.Fx
	ring.CFrame = fireball.CFrame
	ring.RingAttachment.Ring:Emit(50)
	
	h.JumpHeight = 7.2
	h.WalkSpeed = 16
	
	fireball:Destroy()  
	floorfire.RingAttachment.Ring.Enabled = false
	debris:AddItem(floorfire,1)
	debris:AddItem(ring,2)
end

local canShootFireball = true



local function fireball(data)-- projectile
	local c = data.c
	local h = c.Humanoid
	local root = c.HumanoidRootPart
	
	local fireball = fx.Fireball:Clone() 
	fireball.Parent = workspace.Fx
	fireball.CFrame = c.HumanoidRootPart.CFrame * CFrame.new(1.5,0,-5)
	fireball.Attach.Fire.Enabled = true 

	
	local connection
	local count = 0
	
	connection = game:GetService("RunService").RenderStepped:Connect(function(dt)
		if count > 3 then 
			connection:Disconnect()
			fireball.Attach.Fire.Enabled = false
			wait(1)
			fireball:Destroy()
			return
		elseif raycast(fireball.Position, data.endpoint,{c, workspace.Fx}, 100 * dt, fireball) == false then
			for i = 0, 15 do 
				local part = Instance.new("Part",workspace.Fx) 
				part.Size = Vector3.new(4, math.random(15,25)/10, math.random(15,25)/10)
				part.Anchored = true
				part.CanCollide = false
				part.CanQuery = false
				part.CanCollide = false 
				part.CanTouch = false
				part.CFrame = CFrame.new(fireball.Position) * CFrame.Angles(0, math.rad(i * 24), 0) * CFrame.new(0, 0, -8) * CFrame.Angles(math.rad(35), 0, 0)
				
				local params = RaycastParams.new()
				params.FilterType = Enum.RaycastFilterType.Blacklist
				params.IgnoreWater = true
				params.FilterDescendantsInstances = {c, workspace.Fx}
				
				local result = workspace:Raycast(part.Position + Vector3.new(0,2,0), Vector3.new(0,-10,0), params)
				if result then 
					part.Position = result.Position
					part.Material = result.Material
					part.Color = result.Instance.Color	
				end
				
				if i % 3 < 2 then
					local fire = fx.Fireball:Clone() 
					fire.Size = Vector3.new(math.random(10,20)/10,math.random(10,20)/10,math.random(10,20)/10)
					fire.Position = result.Position + Vector3.new(0,3,0)
					fire.Material = result.Material
					fire.Color = result.Instance.Color
					fire.Attach.Fire.Enabled = true
					fire.Attach.Fire.LockedToPart = false
					fire.Parent = workspace.Fx
					fire.Anchored = false
					fire.CanCollide = true
					
					local bv = Instance.new("BodyVelocity", fire) 
					bv.Velocity = Vector3.new(math.random(-40,40), 30, math.random(-40,40))
					bv.MaxForce = Vector3.new(99999,999999,999999)
					bv.Name = "Velocity"
					
					game.Debris:AddItem(bv,.1)
					game.Debris:AddItem(fire,4)
					
					spawn(function()
						wait(2)
						fire.Attach.Fire.Enabled = false
						ts:Create(fire,TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0 , false, 0) ,{Transparency = 1}):Play()
					end)
					
					fire.Transparency = 0 	
				end
				
				part.Position = part.Position - Vector3.new(0,4,0)
				ts:Create(part, TweenInfo.new(.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0, false, 0),{Position = part.Position + Vector3.new(0,4,0)}):Play()
				
				spawn(function()
					game.Debris:AddItem(part,4)
					wait(3)
					ts:Create(part, TweenInfo.new(.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0, false, 0),{Position = part.Position + Vector3.new(0,-4,0)}):Play()
				end) 
			end
			
			fireball.Attach.Fire.Enabled = false
			fireball.Attach.Explosion:Emit(100)
			
			camShake:ShakeOnce(2,5,.2,.4)
			connection:Disconnect()
			if data.client == true then
				data.endpoint = fireball.Position
				remote:FireServer("FireballHit",data)
			end
			wait(1)
			fireball:Destroy()
			
			return
		end
		
		count = count + dt
	end)
end



uis.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.E then
		if mouse.Hit then 
			local data = {client = true, c = character}  
			local load = humanoid:LoadAnimation(anims.Fireball)
			load:Play()  
			local sound = rs.Attacks.Fire.Fx.Circle.fireball
			sound.Playing = true
			debris:AddItem(load, load.Length)  
			remote:FireServer("FireCharge", data)  
			fireCharge(data)
			data.endpoint = mouse.Hit.Position      
			remote:FireServer("Fireball", data)    
			fireball(data)
		end
	end
end)

remote.OnClientEvent:Connect(function(data)
	if data.Attack == "Fireball" then
		fireball(data)
	elseif data.Attack == "FireCharge" then
		fireCharge(data)
	end
end)```

https://gyazo.com/a26acf2ae5d0d737558529b7e2211374

heres a gif how it works if that helps you

1 Like

Hopefully this helps, even though I don’t know your entire code…

local cooldown = 5
local boolean = false

local function Function() -- this line isn't needed
	if gpe or boolean then 
		return 
	end
	if input.KeyCode == Enum.KeyCode.E then
		if mouse.Hit then
			boolean = true
			local data = {client = true, c = character}  
			local load = humanoid:LoadAnimation(anims.Fireball)
			load:Play()  
			local sound = rs.Attacks.Fire.Fx.Circle.fireball
			sound.Playing = true
			debris:AddItem(load, load.Length)  
			remote:FireServer("FireCharge", data)  
			fireCharge(data)
			data.endpoint = mouse.Hit.Position      
			remote:FireServer("Fireball", data)    
			fireball(data)
			task.wait(cooldown)
			boolean = false
		end
	end
end -- this line isn't needed
1 Like

Nice! And this is how your code should look like (I assume that at this moment, your fireball works correctly)

local canShootFireball = true

uis.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.E and canShootFireball then
                canShootFireball = false
		if mouse.Hit then 
			local data = {client = true, c = character}  
			local load = humanoid:LoadAnimation(anims.Fireball)
			load:Play()  
			local sound = rs.Attacks.Fire.Fx.Circle.fireball
			sound.Playing = true
			debris:AddItem(load, load.Length)  
			remote:FireServer("FireCharge", data)  
			fireCharge(data)
			data.endpoint = mouse.Hit.Position      
			remote:FireServer("Fireball", data)    
			fireball(data)

                        —Extra code
                        delay(3,function()
                              canShootFireball = true
                        end)
		end
	end
end)

Please change some indentations also, I typed it from my phone, so it is pretty hard to do so.

1 Like

Thanks now it works properly!!!

1 Like

Nice! Glad I could help! Enjoy coding!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.