How to add all the players who touching a part into a list?

I think its all clear. I need to get a list of players who touch a part via function and to start another function if the amount of players reaches 10.
I have this script atm

‘’'lua
function private(partTouched)
for i = 1,12 do
if partTouched.Name == “HumanoidRootPart” then
local isPlayer = Players:GetPlayerFromCharacter(partTouched.Parent)
if isPlayer then
if not table.find(PlayersActivatedTouchTP, isPlayer) then
table.insert(PlayersActivatedTouchTP, isPlayer)
if Players:GetPlayerByUserId(isPlayer.UserId) then
print({isPlayer})

			end
			end
			end
	end
end
Part.CanTouch = false
wait(1)
connnect(partTouched)

‘’’

You can use .Touched:Connect() in order to detect when someone is touching it. By combining this with :GetTouchingParts() you’ll be able to get all parts touching your part. You can then iterate through that list and get all players from that and if that list of players is >= 10 then you call that function.

1 Like
local Players = game:GetService("Players")

local touchedPlayers: {Player} = {}

local function handleTouch(part: Part): ()
    -- Try to find a Model instance in the hierarchy, then check if it has a
    -- Humanoid instance.
    local model = part:FindFirstAncestorOfClass("Model")
    if not (model and model:FindFirstChildOfClass("Humanoid")) then
        return
    end

    -- Get the Player from the Model instance, return if we don't get one or
    -- we already have that player in our table.
    local player = Players:GetPlayerFromCharacter(model)
    if not player or table.find(touchedPlayers, player) then
        return
    end

    table.insert(touchedPlayers, player)
    if #touchedPlayers == 10 then -- You may want to use >= instead.
        -- Do what you want to do once it reaches 10 here. Optionally use a
        -- guard clause instead (return if #touchedPlayers ~= (or <) 10).
    end
end

Part.Touched:Connect(handleTouch)

-- Remove players from the table if they leave the game.
Players.PlayerRemoving:Connect(function(player)
    local i = table.find(touchedPlayers, player)
    if i then
        table.remove(touchedPlayers, i)
    end
end)
2 Likes

I don’t think this will work, you are never removing players when they stop touching the part, so either you could do a .TouchEnded() and remove them when they stop touching it (which will require you to loop through all touching parts and make sure none of them are a descendant of the player character) or you could perform the calculations from scratch (iterate through all touching parts) whenever .Touched() is triggered

1 Like

Well if they want players actively touching the part, then yes, all they have to do is add a TouchEnded listener that finds players the same way and removes them if they exist in the table.

1 Like

I assumed it was regarding actively touching a part, but re-reading it now I see that it may be interpreted in both ways.

2 Likes

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