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

Teleporting players onto different teams from lobby?

Asked by
Yorixk 0
6 years ago

My code chooses a random map and then teleports users from a lobby team onto the two spawns on the map.

The players are randomly teleported. How can I change their teams without making the spawns "Allow team change on touch"? The teams should also be balanced.

After the round ends, how can I teleport them back to the lobby as well?

Thanks!

local replicatedstorage = game:GetService("ReplicatedStorage")
local status = replicatedstorage:WaitForChild("StatusValue")

while true do
------------------------
-- || INTERMISSION || --
------------------------
  for i = 50,0,-1 do
    status.Value = "Intermission: "..i
    wait(1)
  end
  status.Value = "Choosing map"
  wait (7)
  local maps = game.ServerStorage.Maps:GetChildren()
  local storage = workspace.MapStorage
  storage:Clone().Parent = workspace
  local random_map = maps[math.random (1, #maps)]
  random_map:Clone().Parent = storage
  status.Value = "Map Chosen: " .. random_map.Name 
  wait (5)

----------------------
-- || TELEPORTER || --
----------------------
spawns = random_map.Spawns:GetChildren()
  for i, v in pairs(game.Players:GetPlayers()) do
   name = v.Name
   check = game.Workspace:FindFirstChild(name)
   if check then
    humanoid = check:FindFirstChild("UpperTorso")
    if humanoid then
     check:MoveTo(spawns[i].Position)
      end
    end
  end


  status.Value = "Game in progress"

  wait (420) -- 7 Minute Round

  game.Workspace.MapStorage:Destroy()
  status.Value = "Game Over!"
end

1 answer

Log in to vote
1
Answered by 6 years ago

There are two ways to change teams.


The first is to change the player's TeamColor property to that of a corresponding TeamColor in a team. Note that to set the player's team using TeamColor, you may need to disable the Neutral property of the player first.

The second is to set the player's Team property to the corresponding Team object in the Teams service.

-- TeamColor
Player.Neutral = false
Player.TeamColor = game.Teams.Team1.TeamColor

-- Team
Player.Team = game.Teams.Team1

You can also set the player's RespawnLocation property to a specific SpawnLocation so that the next time their character spawns, that will be the location they spawn at.

Player.RespawnLocation = game.Workspace.SpawnLocation

Hope I helped!

~TDP

0
Thanks for the reply. How would I do this with more than one player? Yorixk 0 — 6y
0
Use 'game.Players:GetPlayers()'. That method returns an table of players, so you can just loop through the table. TheDeadlyPanther 2460 — 6y
0
One more thing, because the server will have multiple players, how would I change the players' teams so that they are balanced? Yorixk 0 — 6y
0
Make each team have #players divided by #teams (perform 'math.floor()' on it so it is a whole number). To get the remaining players, divide using '%' instead of '/'. Then go through the remaining players and stick them in different teams. It's a bit complicated. TheDeadlyPanther 2460 — 6y
0
Ok, thanks. Yorixk 0 — 6y
Ad

Answer this question