Detecting amount of players on a part

Hey! I have a door that teleports players but I want it to function so when a said amount of players are on a part, it’ll close this door by enabling an invisible wall.

This is how I have it scripted so far:

function touch(hit)
	local players = game.Players:GetChildren()
		if #players >= 1 then


			game.Workspace.BARRIERA.CanCollide = true


		end



		local	players = game.Players:GetChildren()
		if #players >- 0 then


			game.Workspace.BARRIERA.CanCollide = false


		end
		
end
script.Parent.Touched:connect(touch)

image

And the part is located in workspace that needs the collisions changed and named “BARRIERA”
No errors are being printed at all, any help is very appreciated!

I’m not sure if this is the problem, but looks like you aren’t using the right comparisons.

For the first one you put “>=” which means “greater than or equal to”

For the second one you put “>-” which means nothing I’m pretty sure it’s a typo

1 Like
function touch(hit)
	local players = game.Players:GetChildren()
	if #players > 1 then


			game.Workspace.BARRIERA.CanCollide = true


		end



		local	players = game.Players:GetChildren()
	if #players < 0 then


			game.Workspace.BARRIERA.CanCollide = false


		end
		
end
script.Parent.Touched:connect(touch)

I think you may be right? I think this is correct, but then when joining it’ll lock the door even if nobody is standing on the point

Uh you are just getting a list of players in the Players, not the ones touching the door.
This should work:

function touch(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if humanoid then
           if humanoid > 0 then
              game.Workspace.BARRIERA.CanCollide = true
           end
      else
      game.Workspace.BARRIERA.CanCollide = true
        end
script.Parent.Touched:connect(touch)

Do you want the door locked or unlocked when a player is standing on the point?

Also did the script return any errors?

Ah. How would I go by working out which players are touching doors?

No errors, and it should work so if two players are standing on a part let’s say, the door will lock. If they leave (or get teleported off in this instance) it’ll open again to allow another 2 players in but I believe @NeoGaming_RBLX may have found the solution.

Yeah, I see that you aren’t using the “hit” parameter at all. You are detecting the actual player object and not the physical people that hit it.

Try using the script I edited into my earlier reply

Thanks! Just tried, no errors but still not functioning

function touch(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		if humanoid > 0 then
			game.Workspace.BARRIERA.CanCollide = true
			print("Test")
		end
	else
		game.Workspace.BARRIERA.CanCollide = true
		
	end
	script.Parent.Touched:connect(touch)
end

No errors or messages printed.

I still don’t fully understand what’s going on in your physical game. Mind sending a screenshot of the barrier and hit detector?

OH I THINK I FOUND IT dont use >0 because even if just one player is on it then it blocks the player (can collide true)
use > 1 or > 2
also you set both can collide statements to true so in any way it would always be true

Maybe because you didn’t connect the function

script.Parent.Touched:connect(touch)

He connected it below:

So that isn’t the error although there is a shorter way of connecting functions that uses less lines

Still getting nothing, it doesn’t even turn collisions to true. Thanks for all the help, no idea what’s happening.

He connected it inside the function. I think he has to connect it after the function loop outside of it.

If you see other devs use this way you’ll find that it usually is inside the function.

This somewhat helped… getting this error now:

Yup. the humanoid is an instance. somehow gotta figure out how many people are touching it.

1 Like

Edited my post, try it and check
There I put the debounce to prevent mega freeze