How can i make a cooldown?

Ive made a script whereas when an obstacle is touched, the player gets damaged using collection serivce tags, but whenever the player does touch it the obstacle instantly kills them even if in the script it says humanoid.health -= 1, the obstacle just kills them instantly. I tried using a debounce script to make a cooldown for it when it can be damageable again but it didnt work. Does anyone know how to fix this?

CS:GetInstanceAddedSignal("Obstacle"):Connect(function()
	for i, model in pairs(CS:GetTagged("Obstacle")) do
		model.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				local db = false
				if db == false then 
					
				hit.Parent.Humanoid.Health -= 30
				local Bomb = Instance.new("Explosion")
				Bomb.BlastPressure = 100000
				Bomb.BlastRadius = 10
				Bomb.Position = model.Position
				Bomb.Parent = model
			db = true
			wait(4)
			db = false
			end
			end
		end)
	end
end)
1 Like

Just to clarify - should the debounce be per player or global?

1 Like

i want it to be global so it affects everybody

1 Like

so if one player gets damaged, then it doesn’t damage anyone else (including the player) until the 4 second delay is over?

1 Like

yes, thats what i want to happen since theres alot of other obstacles other players can be hit by that dont have the cooldown

1 Like

Ok, given you want it global you should be able to just move the local db = false on line 5 of the code snippet you sent, to line 3 (just before model.Touched)

1 Like

or like this

CS:GetInstanceAddedSignal("Obstacle"):Connect(function()
	for i, model in pairs(CS:GetTagged("Obstacle")) do
		local db = false
		model.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				if db == false then
					db = true
					hit.Parent.Humanoid.Health -= 30
					local Bomb = Instance.new("Explosion")
					Bomb.BlastPressure = 100000
					Bomb.BlastRadius = 10
					Bomb.Position = model.Position
					Bomb.Parent = model
					wait(4)
					db = false
				end
			end
		end)
	end
end)

(basically what he said)