How would I make a script that when enough players are in the server it teleports them to a different area to begin the game. Total scrub at coding and I've been reaching a lot of struggles in my projects. You're probably going to see me asking dumb questions like these, so buckle up cherry ;).
I'm sure this isn't the most efficient way, but you could do something like this:
local players = 0 -- Creates a variable that will account for players in the game game.Players.PlayerAdded:connect(function(player) -- called when a player joins the game players = players + 1 if players >= 10 then --if the players is equal to or greater than 10 then do.... --teleportation code here end end)
You could go a step further by using the PlayerRemoving event to see when a player has left the game.
i.e.
game.Players.PlayerRemoving:connect(function(player) players = players - 1 end)
All of you guys answers are pretty legit, but you also need to take into account the game crashing on one of the clients. I'd probably do it like:
ServerScriptService >> Script local max = game.Players.MaxPlayers local go = false
while true do local num = #game.Players:GetChildren() or local num = game.Players.NumPlayers if num >= max and go == false then go = true startgame() --This can be whatever you want. A remote function,etc end wait() --Don't forget this otherwise the game will break end
In my opinion, you should probably start learning what the services do so you won't have to use while loops often. I seldomly use them and mainly use the run service. And if you want to teleport them then
for i, v in pairs(game.Players:GetChildren()) do local char = v:FindFirstChild("Character") if char then char.Torso.CFrame = spwn.CFrame * CFrame.new(0,3,0) --So the torso isn't stuck inside of the spawn or you can just set spawn's cancollide to false or spawn them under spawn also don't name the spawn spawn with a small s since that's a global function or something end end
Also, just wanting to help but there is another easy way. You can do this by using 'NumPlayers'
local MinPlayers = 2 --So this is how many players you would need to start the game. while wait() do --The while loop for your game if game.Players.NumPlayers >= 2 then --code else --If there are not 2 or more players it will print something. print 'Not enough players' end end