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

Putting players into teams?

Asked by 9 years ago

Okay, so I am making a game using aspects of lua I have never come across before. I want to put two people into the team of 'Hunters', and the rest in 'Runners'. The hunters should be random. Could anyone give me a hand with this one?

2 answers

Log in to vote
1
Answered by 9 years ago

Well to get players teamcolor it requires to do player.TeamColor and to set it you can do player.TeamColor = team.TeamColor so do this to set and check colors:

local hunters = game.Teams.Hunters --Add a team called hunters
local runners = game.Teams.Runners --Same thing for runners
local lobby = game.Teams.Lobby --Same thing again

--To get the two hunters
for i = 1, 2 do
    local plr = game.Players:GetChildren()[math.random(1, #game.Players:GetChildren())]
    repeat plr = game.Players:GetChildren()[math.random(1, #game.Players:GetChildren())] until plr.TeamColor == lobby.TeamColor
    --This works if you have a variable for a team Lobby, can be changed easily. Also the repeat until thing is to make sure the player isn't assigned to another team and so that it finds two different hunters
    plr.TeamColor = hunters.TeamColor
end

--To set the rest to runners
for _, v in pairs(game.Players:GetChildren()) do
    --If you are not familiar with for in pairs it loops through the table and _ is the index of the table and the v is the current value at that index
    if v.TeamColor == lobby.TeamColor then
        v.TeamColor = hunter.TeamColor
    end
end

This SHOULD work, otherwise comment any errors

0
Finding a few errors, whenever I try to fix them, another one occurs. The main issue is probably the fact that a blue zig-zag line appears under lobby and hunters :/ Nitrobullet 0 — 9y
1
--This works if you have a variable for a team Lobby, can be changed easily. BosswalrusTheCoder 88 — 9y
1
You need to make a variable for lobby and hunter. BosswalrusTheCoder 88 — 9y
0
Exactly what Boss said, you need to have variables for each team and have the variables be set to the teams VariadicFunction 335 — 9y
0
This is a much neater, loop-free way to get two random players (also easily extensible to get more, even variable numbers): http://hastebin.com/wunofazeyu.lua BlueTaslem 18071 — 9y
Ad
Log in to vote
0
Answered by 9 years ago



local hunters = game.Teams.Hunters local lobby = game.Teams.Lobby local runners = game.Teams.Runners if game.Players<=3 then local players = game.Players:GetPlayers() local one = table.remove(players, math.random(#players)); local two = table.remove(players, math.random(#players)); players.TeamColor = hunters.TeamColor for i,v in pairs(game.Players:GetChildren()) do if v.TeamColor==lobby.TeamColor then v.TeamColor=hunters.TeamColor end end end

Should this work then?

0
No, you can't set a tables TeamColor, also when you are trying to do that you are wanting to set the hunters to a certain team so you'd do two separate lines with stuff like 'one.TeamColor = hunters.TeamColor' and the same for two since those are the two hunters. Also for the 'for loop' if you have it loop through the players table instead ('pairs(players)') then it would already have all the runn VariadicFunction 335 — 9y

Answer this question