How do I have a part detect if a player is standing on it?

So, I am trying to make a queue area that when a player steps on the part, it adds 1 to a player count. And when the player leaves, it removes 1 from a player count.

2 Likes

I would try using :GetTouchingParts(). If that doesn’t work, just try a touched event and add the players touching the part to a table, then loop through it.

https://developer.roblox.com/en-us/api-reference/function/BasePart/GetTouchingParts

2 Likes

I think you are talking about part.Touched and part.TouchEnded

part.Touched:Connect(function(player)
      playerCount = playerCount + 1
end)

part.TouchEnded:Connect(function(player)
      playerCount = playerCount - 1
end)
2 Likes

TouchEnded Is known for being unreliable and shouldn’t be used in a situation like this. Or you would need some form of debounce

4 Likes

U have to use Touched and TouchEnded Event.

Try something like this:
(I didn’t used Debounce in this script, u should add it, here’s a link for more info: Debounce Patterns | Roblox Creator Documentation)

local area = script.Parent -- or type the location where the part is.
local plrNum = 0
local IsTouched = false

area.Touched:Connect(function(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h ~= nil then
		if IsTouched == false then
			IsTouched = true
			plrNum = plrNum + 1
		end
	end
end)

area.TouchEnded:Connect(function(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h ~= nil then
		if IsTouched == true then
			IsTouched = false
			plrNum = plrNum - 1
		end
	end
end)

1 Like

Definitely need some form of debounce if a players presence is supposed to stay registered for a future event. This is easy to get wrong. I’m going to try polling GetTouchingParts() every second because as this video shows, changing idle animation causes TouchEnded to fire after Touch:

I could overlook this for ages while using a single, custom, idle animation (shown in background).