How to prevent a part from being touched multiple times with it not taking into account accesories

  1. I want to make it so that a part cannot being touched again while being touched but my technique doesn’t work.

  2. So I have a script that prints “test1” whenever the part is touched and I want to make it so that test1 cannot be printed multiple times while the part is already touched. (make test1 only print when the player touches the part for the first time.)

  3. I have tried looking into other posts which some worked but as soon as I added a tiny bit of code to make the part not touch accesories, everything broke.

HERE IS THE CODE :

local available = true

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:IsA("Accessory") then return end
	if not hit.Parent:GetAttribute("Ragdoll") and available == true then
		if available == true then
			available = false
		end
		print(hit.Parent)
		print("test1")
	end
end)


script.Parent.TouchEnded:Connect(function(hit)
	if hit.Parent:IsA("Accessory") then return end
	if not hit.Parent:GetAttribute("Ragdoll") and available == false then
		if available == false then
			available = true
		end
	end
end)

Assuming this is a player character thing(you don’t want NPC’s or random parts firing it and such):

local Players = game:GetService("Players") 

local connection
connection = script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstAncestorWhichIsA("Accessory") then return end
	if hit.Parent:FindFirstAncestorWhichIsA("Hat") then return end
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if not player then return end 
	connection:Disconnect()
	print(hit.Parent)
	print("test1")
end)

However, if you want it to be once per player instead of who touches it first you need to use debounces:

local Players = game:GetService("Players") 

local debounces = {} 

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstAncestorWhichIsA("Accessory") then return end
	if hit.Parent:FindFirstAncestorWhichIsA("Hat") then return end
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if not player or table.find(debounces, player) then return end 
	table.insert(debounces, player)
	print(hit.Parent)
	print("test1")
end)

Players.PlayerRemoving:Connect(function(player)
	local index = table.find(debounces, player)
	if index then table.remove(debounces, index) end
end)
1 Like

Not really enough information to answer your question …
Here is baseicly how a debouce (first time flag) works.

local available = true
local ftf = false --> debounce
script.Parent.Touched:Connect(function(hit)
	if ftf == false then ftf = true --> logic lockout
		available = not available

		print(hit.Parent) print(available)

		task.wait(2)
        ftf = false --> reset lockout
	end 
end)

That table debounce Nyrion added, is a thing of beauty. This is just the basic concept to make it a bit easier to understand. This method simply pauses a bit between tests. Works in most cases. This would need added tests like the ones you have already.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.