Cooldown not working

Hello.

Earlier today, I followed a short tutorial to make a throwable projectile. It all works fine, apart from one small thing; you can quite easlily spam projectiles. I tried to implement a cooldown script, but failed. No errors in the output, and I just can’t throw projectiles at all. This is the script after I implemented cooldowns: (You will be able to see which parts I made because they are clearly named with variables.)

local event = script.Parent:WaitForChild("event")
local cooldown = 2
local oncooldown = false

while true do
	if oncooldown == true then
		wait(cooldown)
		oncooldown = false
	end
	wait(0.1)
end


event.OnServerEvent:Connect(function(player)
	if oncooldown == false then
	local char = player.Character
	local HRP = char.HumanoidRootPart
	
	local part = game.Workspace.rock:Clone()
	
	part.Touched:Connect(function(Hit)
		if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
			Hit.Parent.Humanoid.Health = Hit.Parent.Humanoid.Health - 5
		end
	end)
	
	part.Name = "flyingrock"
	part.Size = Vector3.new(2,2,2)
	part.CFrame = HRP.CFrame * CFrame.new(0,0,-5)
	part.Parent = workspace
	
	local att = Instance.new("Attachment")
	local fire = Instance.new("Fire")
	fire.Parent = part
	att.Parent = part
	local LV = Instance.new("LinearVelocity")
	LV.MaxForce = 9999999
	LV.Attachment0 = att
	LV.VectorVelocity = HRP.CFrame.LookVector * 200
	LV.Parent = att
		game.Debris:AddItem(part,1)
	end
end)

Thanks!

I would suggest adding the cooldown after using the projectile.

local event = script.Parent:WaitForChild("event")
local cooldown = 2
local oncooldown = false

event.OnServerEvent:Connect(function(player)
	if oncooldown == false then
    oncooldown = true
	local char = player.Character
	local HRP = char.HumanoidRootPart
	
	local part = game.Workspace.rock:Clone()
	
	part.Touched:Connect(function(Hit)
		if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
			Hit.Parent.Humanoid.Health = Hit.Parent.Humanoid.Health - 5
		end
	end)
	
	part.Name = "flyingrock"
	part.Size = Vector3.new(2,2,2)
	part.CFrame = HRP.CFrame * CFrame.new(0,0,-5)
	part.Parent = workspace
	
	local att = Instance.new("Attachment")
	local fire = Instance.new("Fire")
	fire.Parent = part
	att.Parent = part
	local LV = Instance.new("LinearVelocity")
	LV.MaxForce = 9999999
	LV.Attachment0 = att
	LV.VectorVelocity = HRP.CFrame.LookVector * 200
	LV.Parent = att
		game.Debris:AddItem(part,1)
    task.wait(cooldown)
    oncooldown = false
	end
end)
1 Like

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