Cooldown script wont work

I’m trying to make a cool down script so that the u can shoot a fireball, wait 2 seconds, then shoot another. Though the cool down doesn’t work. “CanShoot” is the cool down.

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local root = character:WaitForChild("HumanoidRootPart")
local canshoot = true
if canshoot == true then
	

function OnActivation()
	

	print('Activation works')
	local ball = Instance.new("Part")
	ball.Shape = Enum.PartType.Ball
	ball.Color = Color3.new(1, 0.333333, 0)
	ball.Size = Vector3.new(2,2,2)
	ball.Material = Enum.Material.Granite
	ball.CanCollide = false

	local fire = Instance.new("Fire")
	fire.Parent = ball
	ball.Parent = root

	local newCFrame = root.CFrame
	local cf = ball.CFrame

	ball.CFrame = newCFrame


	local Velocity = Instance.new("BodyVelocity")

	Velocity.maxForce = Vector3.new(math.huge, math.huge, math.huge) 
	Velocity.Velocity = ball.CFrame.lookVector * 30


	Velocity.Parent = ball
	
	canshoot = false
		
		ball.Touched:connect(function(hit) 
		if hit.Parent:FindFirstChild('Humanoid') and hit.Parent ~= player.Character  then
			local humanoid1 = hit.Parent:FindFirstChild('Humanoid')
			humanoid1:TakeDamage(50)
			local boom = Instance.new("Explosion")
			boom.Parent = ball
			boom.Position = ball.Position
	
			boom.BlastPressure = 500
			boom.BlastRadius = 3
			boom.ExplosionType = "NoCraters"
			boom.DestroyJointRadiusPercent = 0
			
			ball.CanCollide = false
			ball.CanTouch = false
			ball.Transparency = 1
			fire:Destroy()
	
			end
	end)
	ball.Touched:connect(function(hit) 
		if hit:IsA("Part")  and hit.Parent ~= player.Character then
			local boom = Instance.new("Explosion")
			boom.Parent = ball
			boom.Position = ball.Position

			boom.BlastPressure = 500
			boom.BlastRadius = 3
			boom.ExplosionType = "NoCraters"
			boom.DestroyJointRadiusPercent = 0

			ball.CanCollide = false
			ball.CanTouch = false
			ball.Transparency = 1
			fire:Destroy()
			
		end
		
	end)
	end

end
	
	
tool.Activated:Connect(OnActivation)

if canshoot == false then
	wait(3)
	canshoot = true
end

I mention that
if canshoot == true then is outside the actual function…?
Put it inside the OnActivation function

EDIT: Give me a sec let me clean this up for you.

Try this!

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local root = character:WaitForChild("HumanoidRootPart")
local canshoot = true


function OnActivation()

	if canshoot == true then
		canshoot = false
		print('Activation works')
		local ball = Instance.new("Part")
		ball.Shape = Enum.PartType.Ball
		ball.Color = Color3.new(1, 0.333333, 0)
		ball.Size = Vector3.new(2,2,2)
		ball.Material = Enum.Material.Granite
		ball.CanCollide = false

		local fire = Instance.new("Fire")
		fire.Parent = ball
		ball.Parent = root

		local newCFrame = root.CFrame
		local cf = ball.CFrame

		ball.CFrame = newCFrame


		local Velocity = Instance.new("BodyVelocity")

		Velocity.maxForce = Vector3.new(math.huge, math.huge, math.huge) 
		Velocity.Velocity = ball.CFrame.lookVector * 30


		Velocity.Parent = ball
		
		
		wait(2)
		canshoot = true

		ball.Touched:connect(function(hit) 
			if hit.Parent:FindFirstChild('Humanoid') and hit.Parent ~= player.Character  then
				local humanoid1 = hit.Parent:FindFirstChild('Humanoid')
				humanoid1:TakeDamage(50)
				local boom = Instance.new("Explosion")
				boom.Parent = ball
				boom.Position = ball.Position

				boom.BlastPressure = 500
				boom.BlastRadius = 3
				boom.ExplosionType = "NoCraters"
				boom.DestroyJointRadiusPercent = 0

				ball.CanCollide = false
				ball.CanTouch = false
				ball.Transparency = 1
				fire:Destroy()

			end
		end)
		ball.Touched:connect(function(hit) 
			if hit:IsA("Part")  and hit.Parent ~= player.Character then
				local boom = Instance.new("Explosion")
				boom.Parent = ball
				boom.Position = ball.Position

				boom.BlastPressure = 500
				boom.BlastRadius = 3
				boom.ExplosionType = "NoCraters"
				boom.DestroyJointRadiusPercent = 0

				ball.CanCollide = false
				ball.CanTouch = false
				ball.Transparency = 1
				fire:Destroy()

			end

		end)
	end

end


tool.Activated:Connect(OnActivation)
2 Likes

hey it works! I had the right script just wrong location. thanks!

@NDavis06 Actually i just realised it works but it stops any other scripts in the if statement after the wait(2)

Wait i actually fixed it, you had the wait(2) and the other thing in the wrong place

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local root = character:WaitForChild("HumanoidRootPart")
local canshoot = true


function OnActivation()

	if canshoot == true then
		canshoot = false
		print('Activation works')
		local ball = Instance.new("Part")
		ball.Shape = Enum.PartType.Ball
		ball.Color = Color3.new(1, 0.333333, 0)
		ball.Size = Vector3.new(2,2,2)
		ball.Material = Enum.Material.Granite
		ball.CanCollide = false

		local fire = Instance.new("Fire")
		fire.Parent = ball
		ball.Parent = root

		local newCFrame = root.CFrame
		local cf = ball.CFrame

		ball.CFrame = newCFrame


		local Velocity = Instance.new("BodyVelocity")

		Velocity.maxForce = Vector3.new(math.huge, math.huge, math.huge) 
		Velocity.Velocity = ball.CFrame.lookVector * 30


		Velocity.Parent = ball




		ball.Touched:connect(function(hit) 
			if hit.Parent:FindFirstChild('Humanoid') and hit.Parent ~= player.Character  then
				local humanoid1 = hit.Parent:FindFirstChild('Humanoid')
				humanoid1:TakeDamage(50)
				local boom = Instance.new("Explosion")
				boom.Parent = ball
				boom.Position = ball.Position

				boom.BlastPressure = 500
				boom.BlastRadius = 3
				boom.ExplosionType = "NoCraters"
				boom.DestroyJointRadiusPercent = 0

				ball.CanCollide = false
				ball.CanTouch = false
				ball.Transparency = 1
				fire:Destroy()

			end
		end)
		ball.Touched:connect(function(hit) 
			if hit:IsA("Part")  and hit.Parent ~= player.Character then
				local boom = Instance.new("Explosion")
				boom.Parent = ball
				boom.Position = ball.Position

				boom.BlastPressure = 500
				boom.BlastRadius = 3
				boom.ExplosionType = "NoCraters"
				boom.DestroyJointRadiusPercent = 0

				ball.CanCollide = false
				ball.CanTouch = false
				ball.Transparency = 1
				fire:Destroy()

			end

		end)
		wait(1)
		canshoot = true

	end

end


tool.Activated:Connect(OnActivation)