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”
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.
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 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)