How to add cooldown

I’m trying to make it so when the player press the key once it puts it on cooldown for 5 seconds but nothing is working

Teleportation.OnServerEvent:Connect(function(plr,mousepos)
	local db = false
	local plr = plr
	local mouse = plr:GetMouse()
	local char = plr.Character
	local HRP = char.HumanoidRootPart
	
	if not db then
		db = true
		HRP.Position = mousepos
		task.wait(5)
		db = false
	end
end)

I’ve tried doing it in a local script but nothing is working.

1 Like

move db out of the function, you’re overwriting it everytime it runs

local db = false
Teleportation.OnServerEvent:Connect(function(plr,mousepos)
	local plr = plr
	local mouse = plr:GetMouse()
	local char = plr.Character
	local HRP = char.HumanoidRootPart
	
	if not db then
		db = true
		HRP.Position = mousepos
		task.wait(5)
		db = false
	end
end)
1 Like

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