Make a Touched event only fire once PER person

Greetings, I have a touched event, however I currently have a debounce to only hit one person when I would like it to hit multiple, is there an easy way to do this?

Current .Touched script I’m using:

	local function Dmg(Hit)
		if not Hit:IsDescendantOf(Player.Character) and Hit and Hit.Parent then
			local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
			if Humanoid and OnCooldown == false then
				OnCooldown = true
				print("Hit!")

			end
		end
	end

This should work:

--//Services
local Players = game:GetService("Players")

--//Variables
local Part = script.Parent

--//Tables
local Debounces = {}

--//Functions
Part.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	
	if player and not Debounces[player] then
		Debounces[player] = true
		
		print("Hit!")
		
		task.wait(1)
		Debounces[player] = false
	end
end)
1 Like
local part -- your part
local cooldowns = {}

part.Touched:Connect(function(part)
	if part.Parent:FindFirstChildWhichIsA("Humanoid") then
		local char = part.Parent
		local plr = game.Players:GetPlayerFromCharacter(char)
		local hum = char.Humanoid

		if plr and not table.find(cooldowns,plr)  then 
			table.insert(cooldowns,plr)
			hum:TakeDamage(30)
			wait(1)
			table.remove(cooldowns,table.find(plr))
		end
	end
end)

Roblox has a new event connection method called :Once which fires once then disconnects forever. Make a new event for every player that joins with that method.

1 Like

When a player fires the .touched script for the first time, add their name to a table

The next time .touched is fired check if the players name is not in the table, else do nothing