No debounce when stepping on part

I want to make a part where when you step on it, it give you a tool and when you’re not it removes it. I wanted to add a debounce so when you step it only gives the tool once but it’s not working.

local part = script.Parent
local tool = game.ServerStorage.ClassicSword:Clone()
local debounceOn = false
local debounceOff = false
local playerUsingTool = nil

part.Touched:Connect(function(hit)
	local character = hit.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	if player and not debounceOn then
		debounceOn = true
		playerUsingTool = player
		tool.Parent = player.Backpack
	end
end)

part.TouchEnded:Connect(function(hit)
	if playerUsingTool and not debounceOff then
		local character = hit.Parent
		local player = game.Players:GetPlayerFromCharacter(character)
		if player == playerUsingTool then
			tool.Parent = game.ServerStorage
			playerUsingTool = nil
			debounceOn = false
		end
	end
end)

You can try this code. It might be because it doesn’t register not Debounce, so to replace that, use Debounce == false, or vice versa if it’s true.

local part = script.Parent
local tool = game.ServerStorage.ClassicSword:Clone()
local debounceOn = false
local playerUsingTool = nil

part.Touched:Connect(function(hit)
	local character = hit.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	if player and debounceOn == false then
		debounceOn = true
		playerUsingTool = player
		tool.Parent = player.Backpack
	end
end)

part.TouchEnded:Connect(function(hit)
	if playerUsingTool and debounceOn == true then
		local character = hit.Parent
		local player = game.Players:GetPlayerFromCharacter(character)
		if player == playerUsingTool then
			tool.Parent = game.ServerStorage
			playerUsingTool = nil
			debounceOn = false
		end
	end
end)

If this is a server sided script, you don’t wanna have a variable for debounces, you’d wanna use a table.

So for example, something like this:

local debounces = {}

part.Touched:Connect(function(hit)
    local player = ...
    if not debounces[player] then
         debounces[player] = true
         ...
         task.wait(1)
         debounces[player] = false

Also if you don’t want them having the tool, you can just check their backpack or their character model to see if they have the tool.

If you use a variable for debounces on the server, no one else can use the button during the duration period

It didn’t work. It doesn’t debounce at all.