So lets say I have six players (Player 1 though 6). Now I have 2 doors(Door 1 and 2). How would I make it so that only a certain number of players can pass through the door before it will not let anyone else through. Lets say Door 1 has a player limit of 4 people. How would I make it so that after 4 players go through it, no more players can go through it until one of the original 4 players dies or leaves the server???
COMMUNITY QUESTION
Special Door Help? Player Limit
2 answers
Put them all in a table, because there are multiple parts in a humanoid. Or, you can be fancy with shared/_Global variables.
touchedPlayers = {} --global just in case I messed it up; i don't know about syntax because I'm writing this in the actual scriptinghelpers answer box
MAX_PLAYER_COUNT = 4
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(character)
character:WaitForChild('Humanoid').Died:connect(function(death)
touchedPlayers[player.Name] = nil
end)
end)
end)
game.Players.PlayerRemoving:connect(function(player)
touchedPlayers[player.Name] = nil
end)
game:GetService('RunService').Heartbeat:connect(function(doublestep)
if #touchedPlayers == MAX_PLAYER_COUNT then
v.CanCollide = true
else
v.CanCollide = false
end
end)
script.Parent.Touched:connect(function(hit)
if #touchedPlayers == MAX_PLAYER_COUNT then
return nil
end
Humanoid = hit:FindFirstAncestorOfClass('Model')
if Humanoid and not touchedPlayers[Humanoid.Name] then
if game.Players:FindFirstChild(Humanoid.Name) then
touchedPlayers[game.Players[Humanoid.Name]] = true
end
end
end)
Tell me errors and I'll edit it. Currently can't open roblox studio (and do much on it because of crashing)
1 comment
0
I put the script in a part and it did not work
Here is an idea:
local maxPlayersThrough = 4
local playersThrough = 0
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then -- detects if they are a player
if playersThrough < maxPlayersThrough then
playersThrough = playersThrough + 1
end
end
end)
mind you this script will only work if the same player does not leave the room or touch the door as well as if a player who went through the door leaves the server. Hope this helps! Have a great day scripting!
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
Just count every time it is touched and a player passes through, and set it as collidable after you reach a maximum.
But what if someone who passed through dies or leaves? Their spot has to reopen so someone else could go through