Help with spawning for a team-based game

Hello There (Scripter),

I am new to scripting. So, I am really a bad scripter. So, I tried searching this everywhere but, couldn’t find it. So, I made a post on it. So, I have a game in my game with the maximum players of 10. So, I want one of those 10 players to get in a specific team called (FBI) spawned in a specific area which no one of those 9 players left can spawn there with a custom StarterCharacter with a weapon. Those 9 players will have to spawn in a specific team called (L.E.D) and those players who gets killed by the player in the (FBI) team with the weapon gets spawned in a lobby team which is called (L.E.D.2). That’s all!

Thanks! :heart:

Before starting make sure to have set SpawnLocations for the L.E.D, L.E.D.2 team and the FBI team.

Alright, so firstly we’ll need to pick our random player. We can use Players:GetPlayers() to return all the players in the server as an array. Then you’ll have to pick a random index, like so:

local players = game:GetService("Players"):GetPlayers() --Pretty much returns {player1, player2} etc
local randomIndex = math.random(#players) --One interval is given picks it between 1 and the size of the players array
local randPlayer = players[randomIndex] --get that random player from the array

Now we’ll need to Team the player to the FBI team and re-load them to spawn them at the FBI area:

local teams = game:GetService("Teams") --Get the Teams service
randPlayer.Team = teams:FindFirstChild("FBI") --set their team to the FBI team
randPlayer:LoadCharacter() --Spawn them

Now remove that player from our array:

table.remove(players, randomIndex)

Next we’ll need to set all the other players to the L.E.D team, we can do so by looping through our players array.
We’ll also have to make a connection to when they die so they’ll join the L.E.D.2 team:

for _, ledPlayer in pairs(players) do --for every player in the players array
    ledPlayer.Team = teams:FindFirstChild("L.E.D") --team them to the L.E.D team
    ledPlayer:LoadCharacter() --spawn them
    local connection do --shove the connection in a do block, then we can define and change it with ease
        connection = ledPlayer.Characer:WaitForChild("Humanoid").Died:Connect(function() --connect when they die
            ledPlayer.Team = teams:FindFirstChild("L.E.D.2") --team them to the L.E.D.2 team
            connection:Disconnect() --stop connecting when they die.
       end)
    end
end

If you get any problems or misunderstandings, feel free to ask.

1 Like

So, I am sure that I mentioned that I am new to scripting. So, I kinda not understand those scripts… So, I was thinking if you could make them in the roblox studio then send the file here… I am really sorry so, yeah then I’ll hit the solution button cuz your the first one who helped me find the answer I guess. :heart: :heart: :heart:

I can give you snippets and examples on how to do stuff however I can’t give you scripts straight up as stated in the Scripting Support information:

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

Ohh, okie. There’s no problemeo. Thanks! :heart:

1 Like

So, where do I put this script?

local players = game:GetService("Players"):GetPlayers() --Pretty much returns {player1, player2} etc
local randomIndex = math.random(#players) --One interval is given picks it between 1 and the size of the players array
local randPlayer = players[randomIndex] --get that random player from the array

Well wherever your script starts to choose and set the Teams.

1 Like

Ahhh, I think you understood me wrong. I meant where do I put this script. Do I put it in ReplicatedFirst? Since, ReplicatedFirst starts anything before the game loads up I think so…

Im think to ServerScriptService

1 Like

ReplicatedFirst is a good place to place your first running LocalScripts
You’ll want to put it in a Script in ServerScriptService

1 Like

Finally, I am done with it. So, I insert another Script in ServerScriptSerice. This script.

local teams = game:GetService("Teams") --Get the Teams service
randPlayer.Team = teams:FindFirstChild("FBI") --set their team to the FBI team
randPlayer:LoadCharacter() --Spawn them

Right?

It all should be in one Script.

1 Like

Maybe yes Maybe no idk try try it

1 Like

Do I leave spaces between every script you wrote in the script?

In Lua, whitespace hardly makes a difference to how it’s ran.

2 Likes

Okay, one more thing. Could you combine the script together because I am confused and scared of breaking the script or something… So, I don’t break or get errors in game… Also, your a really great scripter I have never seen before. Reminds me of Badcc. That’s just amazing. Thanks! :heart:

Certainately, here:

local players = game:GetService("Players"):GetPlayers() --Pretty much returns {player1, player2} etc
local randomIndex = math.random(#players) --One interval is given picks it between 1 and the size of the players array
local randPlayer = players[randomIndex] --get that random player from the array

local teams = game:GetService("Teams") --Get the Teams service
randPlayer.Team = teams:FindFirstChild("FBI") --set their team to the FBI team
randPlayer:LoadCharacter() --Spawn them

table.remove(players, randomIndex)

for _, ledPlayer in pairs(players) do --for every player in the players array
    ledPlayer.Team = teams:FindFirstChild("L.E.D") --team them to the L.E.D team
    ledPlayer:LoadCharacter() --spawn them
    local connection do --shove the connection in a do block, then we can define and change it with ease
        connection = ledPlayer.Characer:WaitForChild("Humanoid").Died:Connect(function() --connect when they die
            ledPlayer.Team = teams:FindFirstChild("L.E.D.2") --team them to the L.E.D.2 team
            connection:Disconnect() --stop connecting when they die.
       end)
    end
end

Keep in mind this’ll only run once, so you may want to look into methods of when you want it to be called.

1 Like

Thank you so much! :heart: :heart: :heart:

1 Like

In this part of the Script in ServerScriptService.

for _, ledPlayer in pairs(players) do --for every player in the players array
    ledPlayer.Team = teams:FindFirstChild("L.E.D") --team them to the L.E.D team
    ledPlayer:LoadCharacter() --spawn them
    local connection do --shove the connection in a do block, then we can define and change it with ease
        connection = ledPlayer.Characer:WaitForChild("Humanoid").Died:Connect(function() --connect when they die
            ledPlayer.Team = teams:FindFirstChild("L.E.D.2") --team them to the L.E.D.2 team
            connection:Disconnect() --stop connecting when they die.
       end)
    end
end

Does the “ledPlayer” have to do something with the team called “L.E.D”?

“ledPlayer” acts as each player, who are then Teamed to L.E.D.
I just thought the variable name suited well.

1 Like