Detecting amount of players on a part

My fault, forgot to commit script. Oops.

@HunterGreya Maybe this should work? Try it
Oh I see if it still won’t work try this.

No errors but I believe it’s to do with this:
image

Try putting it in the function itself. the function counts as something like a variable, so you should probably put the connect it inside and maybe put a local in front of the word function like

local function touch(hit)

and the connect should have uppercase c
Connect
idk if it matters but please try it

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

		end
	end
end

^ tried this, no errors nothing.

local humcount = 0
local debounce = true
local function touch(hit)
	if debounce then
		debounce = false
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid then
			humcount = humcount + 1
			if humcount > 1 then
				game.Workspace.BARRIERA.CanCollide = true
			end
		else
			humcount = 0
			game.Workspace.BARRIERA.CanCollide = false
			debounce = true
			end
	end
end

script.Parent.Touched:connect(touch)

I have not tested this. Can you give it a try?

local playersTouching = {}

local function checkPlayer(playerName)
    for i, v in pairs(playersTouching) do
        if v == playerName then
            return true
        end
    end
end

script.Parent.Touched:Connect(function(touchedPart)
    if touchedPart.Parent:FindFirstChildOfClass("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(touchedPart.Parent)
        if checkPlayer(player.Name) ~= true then
            table.insert(playersTouching, player.Name)
        end
    end
end)

script.Parent.TouchEnded:Connect(function(touchedPart)
    if touchedPart.Parent:FindFirstChildOfClass("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(touchedPart.Parent)
        if checkPlayer(player.Name) ~= true then
            table.remove(playersTouching, player.Name)
        end
    end
end)

print(#playersTouching)

Thanks for this. Just copied it over to test it and:
image
the rest of the script looks fine. Is this to do with end(s) / closing the statement?

I updated the script. Please take a look at it.

Thanks:

I updated the script. Please take a look at it again.

Thanks once again, experiencing the same issue but this time with line 21.

Sorry, updated again. I am sorry for the minor mistakes.

1 Like

A 0 is printed upon joining (I’m guessing this is to do with [quote=“Anonymous_2647, post:28, topic:1439511”]

> print(#playersTouching)
> ```
[/quote]

so everything seems functional! Thank you, but from here will I have to use the system as such:

	if #players >- 0 then

to enable/disable the wall?

thanks!

One moment, let me test it out myself. I do not want to give you a wrong answer.

1 Like

I found some errors in the script. I will update it.

1 Like

This is what I came up with and tested, so hopefully it works for you. Adjust the maxWeight variable to the number of players allowed on the part. You’ll also want to implement functions to handle players leaving / moving off the part.

local onPart = {}
local maxWeight = 2

local debounce = false

function touch(hit)
	
	if not debounce then
		
		debounce = true
		
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		
		print(onPart)
		if #onPart == maxWeight then
			print("Max Weight")
		end
		-- Only let players activate
		-- Make sure there is space left
		if humanoid and #onPart < maxWeight then
			
			-- Has player already touched?
			
			for i = 1, maxWeight do
				if humanoid.Parent.Name == onPart[i] then
					-- This player exists
					print("Player Exists")
					return
				end
			end
			
			coroutine.resume(
				coroutine.create(
					function()
						print("Player added")
						-- Add player
						onPart[#onPart + 1] = humanoid.Parent.Name
					end
				)
			)
		end
		
		if maxWeight <= #onPart then
			print("Max weight")
			
			-- Your functions here
			
		end
		
		wait(0.5)
		
		debounce = false
	end
end

script.Parent.Touched:connect(touch)

1 Like

I have not tested this yet but I think it’s a better alternative than to use touch event as it’s not very reliable in handling multiple touches.

local part = script.Parent
local door = game.Workspace.BARRIERA
local amountOfPlayers = 3

local region = Region3.new(part.Position - part.Size/2, (part.Position + part.Size/2) + Vector3.new(0,7,0))

while wait(1) do
    local PlayerCount = 0
    local parts = workspace:FindPartsInRegion3(region,part)
    
    for _, part in pairs(parts) do
        local player = game.Players:GetPlayerFromCharacter(part.Parent)
        if player then
            PlayerCount += 1
        end
    end
    
    if PlayerCount >= amountOfPlayers then
        door.CanCollide = false
        
    else
        wait(2)
        door.CanCollide = true
    end
end
1 Like

Thank you! This works for locking the part, to handle with players leaving/being teleported is it this section that I could adjust to work with that?

	coroutine.resume(
				coroutine.create(
					function()
						print("Player added")
						-- Add player
						onPart[#onPart + 1] = humanoid.Parent.Name
					end
				)
			)
		end
		
		if maxWeight <= #onPart then
			print("Max weight")
			
			-- Your functions here
			
		end

I personally prefer Region3 as well.

2 Likes