Is it possible to detect if a part was touched once?


Explanation

Okay, when I ask Is it possible to detect if a part was touched once; I mean… if a part is touched once, it fires an event and doesn’t continue to detect anything touching the part.

A good example: if I was to press the button E, and have an animation play with it moving a stick, it only detects what the stick is touching while the animation is playing.

Code

The animation currently plays on the client side; I plan to update that once I figure out how to get the stick to only detect once.

This code is in a normal Script in ServerScriptService.

game.ReplicatedStorage.Events.MovementListener.OnServerEvent:Connect(function(plr,Fire)
	if game.Workspace:FindFirstChild(plr.Name) then
		local Character = game.Workspace[plr.Name]
		local Module = require(game.ReplicatedStorage.ModuleScript)
		if string.lower(Fire) == "pokecheck" then
			warn("Fired PokeCheck")
			Character.Handle.Hitbox.Touched:Connect(function(hit)
				local Table2Print = {hit.Parent.Name,hit.Name}
				warn(Table2Print)
				if hit.Parent.Name ~= Character.Name and hit.Name == "gamePuck" and hit:FindFirstChild("WeldConstraint") and hit.WeldConstraint.Enabled == true then
					hit.WeldConstraint.Enabled = false
					hit.WeldConstraint:Destroy()
					wait(.02)
					game.ServerScriptService.ServerScripts["Hockey.Handler"].Configuration.HasPuck = nil
				end
				Module.ClearAnimations(Character.Humanoid)
			end)
		end
	end
end)

Thank you in advance for any help you may offer.

3 Likes

You could add a touch debounce:

-- PSEUDO CODE--

debounce = true

part.Touched:Connect(function(touchedPart)
   --check if debounce is false then return here
    debounce = false
    print(touchedPart.Name)
end)

or if you want it to never detect that part again add it to a table:

-- PSEUDO CODE--

local touchedparts = {}

part.Touched:Connect(function(touchedPart)
    --check if part exists in table already then return here
    table.insert(touchedParts, touchedpart)
    print(touchedPart.Name)
end)
1 Like

I’d work with the :Wait() function on the Touched event or use :Disconnect() on the connection after it triggers once with the intended target

an example of :Wait()

print("A")
local Player1 = game.Players.PlayerAdded:Wait()
print(Player1, "is the first player")

an example of :Disconnect()

local Connection
Connection = Part.Touched:Connect(function(OtherPart)
   local Character = OtherPart:FindFirstAncestorOfClass("Model")
   local Player = game.Players:GetPlayerFromCharacter(Character)
   if Player then
      print("A player has touched")
      Connection:Disconnect()
   else
      print("A non-player has touched")
   end
end)
3 Likes