Debounce Script for One Player Only

The following script is for a laser beam that damages the player on touch.

debounce = false
script.Parent.Touched:Connect(function(hit)
	if not debounce then
	debounce = true
		
		if hit.Parent:FindFirstChild("Humanoid") then
			hit.Parent:FindFirstChild("Humanoid"):TakeDamage(20)
		end
	
	wait(0.5)
	
	debounce = false
	end
end)

Now this debounce applies for everyone, not just the player who touched the laser beam. I’ve thought of making an array for the debounce but I don’t know how to proceed.

Any suggestions? Thanks!

4 Likes

Yeah just have a table with “tagged” humanoids, after you wait just remove the player from the tagged table.

Use a table of players.

local debounces = { }
local Players = game:GetService("Players")

script.Parent.Touched:Connect(function(part)
    local player = Players:GetPlayerFromCharacter(part.Parent)
    
    if debounces[player] then
        return -- If their debounce is still active then return
    end
    
    debounces[player] = true
    player.Character.Humanoid:TakeDamage(20)
    wait(0.5)
    debounces[player] = nil
end)
8 Likes

Use a local script to only affect one player.

Yess, thank you very much incapaxx. This is exactly how I had it in my mind but I couldn’t remember exactly how array tables work.

Change from script to local script.