Touched Event Help

Well, I am trying to make a script that if the player touched a part the script prints “Touched” for example and if the player stop touching the part it prints “Touch ended”.
The problem is that if the player touch the part, and it moves while touching the part, it also prints “Touched ended”

Any help please

Why are you worrying about what is printed?

Because even if the player is touching the part, it also detect that the touched ended

You can add something called a debounce, for example

local db = false
part.Touched:Connect(function(hit)
if db == false then
print()
db = true
wait(5)
db = false
end
end)

Essentially this is sudo code so I don’t know if it’ll still work, but a debounce makes it so that it can only filter one response at a time until the timer ends on it and that it can be done again.

1 Like

The second any part of the player touches or stops touching a part, it will fire the respective event. So, one way to fight this is to use a debounce, or to make a variable that defines if the part is being touched, etc

You can do

> if script.Parent.Touched then
> 	print("asd")
> end

You could use touched but, sometimes I don’t like using that. Usually J use workspace.getPartsInPart it returns an array of all the parts touching or inside of it!

touching = {}
PartTouched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local char = hit.Parent
		local plr = game.Players:GetPlayerFromCharacter(char)
		if not table.find(touching, plr.Name) then
                     table.insert(touching, plr.Name)
                     print("touched")
              end
	end
end)

Part.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local char = hit.Parent
		local plr = game.Players:GetPlayerFromCharacter(char)
                if table.find(touching, plr.Name) then
                     table.remove(touching, table.find(touching, plr.Name))
                     print("touch ended")
              end
	end
end)

Sth like this should work (Sry for weird formating there)

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