How to see how many players are currently touching a part?

Ok, so as the title says I want to know how many players are touching my part at once. Its to change the speed of how fast my payload is.

Firstoff, I’ve tried GetTouchingParts, which returns a table but I didn’t seem to find any players in there. Maybe there are but I was unsure of how to do it. I also tried using .hit and finding the humanoid and checking if = (number), but it didn’t work. Otherwise, currently I am using .touched to loop through the -part which works and I can get the player, but I don’t know how to find out how many are touching it.

5 Likes

If you want to use .Touched you can simply create a table that stores the players that are touching the part.

local Players = game:GetService('Players')

local PlayersTouchingPart = {}

Part.Touched:Connect(function(hit)
      local Player = Players:GetPlayerFromCharacter(hit.Parent)
      if Player and not table.find(PlayersTouchingPart ,Player.Name) then --Check if player exists and if the player is already in the table
            table.insert(PlayersTouchingPart, Player.Name)
      end
end)

Part.TouchEnded:Connect(function()
     local Player = Players:GetPlayerFromCharacter(hit.Parent)
     if Player and table.find(PlayersTouchingPart ,Player.Name) then -- Check if the player is in the table
          table.remove(PlayersTouchingPart, table.find(PlayersTouchingPart, Player.Name))
     end
end)

Then you can use #PlayersTouchingPart to see how many players are in the table

6 Likes

Thank you, this seems to be working just fine.

1 Like