Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to accurately detect when a player fully stops touching a part? [SOLVED]

######had to trim the title quite a bit edit: where are all the comments and answers why do i have only one dude who has commented and answered this

local debounce = false
script.Parent.Touched:Connect(function(otherPart)
	local player = game:GetService("Players"):GetPlayerFromCharacter(otherPart.Parent)
	if player and not debounce then
		debounce = true
		game:GetService("ReplicatedStorage").TouchEvent:FireClient(player, script.Parent)
		script.Parent.TouchEnded:Wait()
		wait(1)
		debounce = false
	end
end)

hold up

what's that I see?

huhr?

i can't read it

lets zoom in

#script.Parent.TouchEnded:Wait()

ok let's be serious now

You'd think, "ey this'll work perfectly"

Well, until you pay attention to this:

##players have multiple parts

Soo...

What's the best way to get when the player actually stops touching the part?

######only revert to player:GetDistanceFromCharacter(script.Parent.Position) if it's the only way out ######and note: plz make sure u include how 2 do it with R15

EDIT: I figured it out, you just have to use :GetTouchingParts() and check if the parents of any of the given values are the player's Character. I will keep this question hanging around so that others can see.

local debounce = true
script.Parent.Touched:Connect(function(otherPart)
	local player = game:GetService("Players"):GetPlayerFromCharacter(otherPart.Parent)
	if player and debounce then
		debounce = false
		game:GetService("ReplicatedStorage").TouchEvent:FireClient(player, script.Parent)
		local function waitUntilNoMoreCollision() -- importent
			while true do
				local s = false -- importent
				for i,v in pairs(script.Parent:GetTouchingParts()) do -- importent
					if v.Parent == player.Character then -- importent
						s = true -- importent
					end
				end
				if not s then -- importent
					print("Player stopped touching!")
					return -- importent
				end
				wait() -- ultra importent
			end
		end
		waitUntilNoMoreCollision() -- importent
		debounce = true
	end
end)

2 answers

I would just do it like this which works perfect everytime.

Code:

script.Parent.TouchEnded:Connect(function(hit)

	if (hit.Parent:IsA("Model")) and (hit.Parent:FindFirstChild("Humanoid")) then 
		
		-- Code

	end
	
end)

This basically checks if the parent of the part it hits is a model, and checks if it's a character by finding the humanoid.

5 comments

0

Err... no. Won't make a difference, for as I mentioned in the question: PLAYERS. HAVE. MULTIPLE. PARTS.

0

and dose parts be MOVING

0

When you say multiple parts what do you mean, because R15 uses multiple parts already, and that works perfect for me

0

When you say multiple parts what do you mean, because R15 uses multiple parts already, and that works perfect for me

0

err... the parts go back and forth and it easily triggers the TouchEnded event with one part and then another part re-triggers the Touched event

local debounce = true
script.Parent.Touched:Connect(function(otherPart)
	local player = game:GetService("Players"):GetPlayerFromCharacter(otherPart.Parent)
	if player and debounce then
		debounce = false
		game:GetService("ReplicatedStorage").TouchEvent:FireClient(player, script.Parent)
		local function waitUntilNoMoreCollision() -- importent
			while true do
				local s = false -- importent
				for i,v in pairs(script.Parent:GetTouchingParts()) do -- importent
					if v.Parent == player.Character then -- importent
						s = true -- importent
					end
				end
				if not s then -- importent
					print("Player stopped touching!")
					return -- importent
				end
				wait() -- ultra importent
			end
		end
		waitUntilNoMoreCollision() -- importent
		debounce = true
	end
end)

yey i am smort and i figured it out mahself

This discussion is preserved as it was. New answers, comments, and votes are closed.