# How Would I Detect If All Players Are Touching One Part?

How Would I Detect If All Players Are Touching One Part? I’ve got a basic idea on this but I am completely sure.

1 Like

I know how basic collisions work I don’t know how to detect if all the players are currently touching it

i would probably give everyone a ‘isTouching’ boolean and set it to true if they’re touching a target

then check if every ‘isTouching’ value is true by looping through all of the values

local function checkValues(touchers)
if not touchers then
warn("there's clearly a problem with the given 'touchers'")
end

for _, touch in ipairs(touchers) do
if not touch.Value then
return false
end
end
return true -- none were false
end

print(tostring(checkValues(currentTouchers)))

if there’s an easier way then i would like to know because i’m curious

1 Like

I’ll try this in the morning it’s late for me but this is a clever way too do it I’ll leave the post open in case there is an easier way till I test it tomorrow

You can call yourPart:getTouchingParts(), which returns an array of each part which is, well, touching the part.

From there you can loop through the array and check which parts belongs to a players and which ones don’t, etc.

What I’d do is create an array containing all the characters touching it, and whenever the length of this array exceeds the amount of players, fire a function or whatever code you want.

local players_touching = {}
local part = script.Parent

local function RunFunction()
print("All players are touching!")
end

part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then-- and hit.Name == "HumanoidRootPart" then
players_touching[hit.Parent.Name] = hit.Parent

local amount_touching = 0

for i, v in pairs(players_touching) do
amount_touching += 1
end

if amount_touching >= #game.Players:GetPlayers() then
RunFunction()
end
end
end)

part.TouchEnded:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then-- and hit.Name == "HumanoidRootPart" then
if players_touching[hit.Parent.Name] then
players_touching[hit.Parent.Name] = nil
end
end
end)

The comments I added is optional code, the function runs many times because some parts of the player switching between touching and not touching, but if you add the code commented then it’ll only count when the humanoidRootPart of the players is touching.

1 Like

This is a very clever solution thank you and everybody else who helped

1 Like

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