My cooldown not working

I’m making a dive script for a game. But it seems that you can just spam it and go into light speed.
I keep trying to patch it, but it prints no errors and ignores the DB.

local db
db=true
local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()

local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/asset/?id=7345732547"
mouse.KeyDown:connect(function(key)
	if key == "y" and db == true then
		
		db=false
		local playAnim = humanoid:LoadAnimation(anim)
		playAnim:Play()
		local vector = Instance.new("VectorForce")
		vector.Parent = game.Players.LocalPlayer.Character.Torso
		vector.Force = Vector3.new(2000,4000,0)
		local attachment = Instance.new("Attachment")
		attachment.Parent = game.Players.LocalPlayer.Character.Torso
		vector.Attachment0 = attachment
		wait(0.5)
		while true do
			wait()
			if game.Players.LocalPlayer.Character.Humanoid.FloorMaterial ~= nil then
				vector.Force=Vector3.new(1000,0,0)
				attachment:Destroy()
				playAnim:Stop()
				wait(0.3)
				vector:Destroy()
				db=true
			end
		end end
end
)

Okay i think i know what the problem is.

while true do
			wait()
			if game.Players.LocalPlayer.Character.Humanoid.FloorMaterial ~= nil then
				vector.Force=Vector3.new(1000,0,0)
				attachment:Destroy()
				playAnim:Stop()
				wait(0.3)
				vector:Destroy()
				db=true
			end

While true do loop makes the db true over and over, it doesn’t stop. which makes the player spam it
(While true do will make a loop loop infinitely)

what exactly is the loop for/doing?

Checks if the floor material not = nil then lands.
Also, I took out the db out of the loop and it didn’t change anything.

that’s because While True do prevents anything from outside the loop from running until the loop itself stops

By the looks of it, you’re better off doing

repeat
wait()
until game.Players.LocalPlayer.Character.Humanoid.FloorMaterial ~= nil

Try this? it may work
(So basically do this)

repeat
wait()
until game.Players.LocalPlayer.Character.Humanoid.FloorMaterial ~= nil
				vector.Force=Vector3.new(1000,0,0)
				attachment:Destroy()
				playAnim:Stop()
				wait(0.3)
				vector:Destroy()
				db=true
			end

It works! Thank you! I tested this and works just as its supposed to.
I’m making a SM64 type game and this’ll help.