Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Enough players to start Script?

Asked by 8 years ago
Edited 8 years ago

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 ;).

0
Here's a tip! Make a while loop with a condition of having not enough amount of players and make a wait() inside of the loop. starlebVerse 685 — 8y
0
If you include the script that you are using, then I will try to modify it. marioblast1244 113 — 8y

3 answers

Log in to vote
1
Answered by 8 years ago

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)
0
Thanks so much! keatenn0987 5 — 8y
0
Of course! Glad I could help! Brinceous 85 — 8y
Ad
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

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

Log in to vote
0
Answered by
FiredDusk 1466 Moderation Voter
8 years ago
Edited 8 years ago

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

Answer this question