|
| 23 Nov 2013 12:27 PM |
Does anyone know how I can make it so every 5 minutes people change teams? Like in WWII~Battle For Carentan~! |
|
|
| Report Abuse |
|
|
GGGGG14
|
  |
| Joined: 29 Jan 2012 |
| Total Posts: 25344 |
|
|
| 23 Nov 2013 12:30 PM |
while true do p = game.Players:GetChildren() for i = 1, #p do if p[i].TeamColor = game.Teams["1"].TeamColor then wait(--[[amount of time]]) p[i].TeamColor = game.Teams["2"].TeamColor elseif p[i].TeamColor = game.Teams["2"].TeamColor then wait(--[[amount of time]]) p[i].TeamColor = game.Teams["1"].TeamColor end end end end --[[team change]] |
|
|
| Report Abuse |
|
|
|
| 23 Nov 2013 12:38 PM |
| Thanks, but there are 6 teams. How would I change it so that it would work? |
|
|
| Report Abuse |
|
|
|
| 23 Nov 2013 12:38 PM |
Yes! To wait five minutes, use the code
wait(5 * 60)
The wait code waits for the number of seconds in the parenthesis, for times as small as 0.03 seconds.
You can then use a while true loop to keep doing this forever,
while true do wait(5 * 60) --code to change teams end
which can be writen more concicely as
while wait(5 * 60) do --change teams end
To change teams you need to iterate through all players and then pick a team that is not theirs! A simple, if not optimally efficient, way to do this would be to use nested for loops.
--change teams: local players = pairs(game.Players:GetChildren()) local teams = pairs(game.Teams:GetChildren()) for _,player in players do for __,team in teams do if player.TeamColor ~= team.TeamColor then player.TeamColor = team.TeamColor break end end end
The for loops work like this:
First create a list of all the players and set it up to be "iterable" (Able to be moved through and return each object in the list). This is done with
local players = pairs(game.Players:GetChildren())
Then we will need to go through the list of teams *for each player*, so we create another iterable list of teams.
After that, use a for i,v in pairs(list) do loop. This works like a normal for loop except it has two values. "i" is the index and "v" is the object. We wont use the indecies, so we assign them a nonsense pointer like "_" and "__".
This code be written in english as:
create a list of players create a list of teams for each player in players for each team in teams if players is not in team put the player in the team stop going through teams if not stopped, go to next team go to next player |
|
|
| Report Abuse |
|
|
|
| 23 Nov 2013 12:40 PM |
| I'll try greendolph's idea. |
|
|
| Report Abuse |
|
|
|
| 23 Nov 2013 12:41 PM |
| 6 teams changes things, will make it slightly more complex. You will need to store the current team value of each player and then set there team color to the color of the "next" team, using current_team%6 + 1 to "wrap around". |
|
|
| Report Abuse |
|
|
|
| 23 Nov 2013 12:49 PM |
| Do you know how to do that? |
|
|
| Report Abuse |
|
|
|
| 23 Nov 2013 12:56 PM |
You will have to create a list of teams in a specific order, a good way to do this would be to add a number at the end of each team, but that can be tacky, so you could add an IntValue to each team with its unique ID with values 1 through 6 and then create an explicitly indexed table to call teams from.
local teams = {} for _, t in pairs(game.Teams:GetChildren()) teams[t.ID.Vaue] = t end
That way teams[4] Will always call the 4th team.
Each player will also need a "current team" value. This can be done something like this..
game.Players.PlayerAdded:connect(function(player) local i = Instance.new("IntValue") i.Name = "Current_Team" for index = 1, #teams do if teams[index].TeamColor == player.TeamColor then i.Value = index end end i.Parent = player end);
Now when you switch teams, iterating through all players and setting their current_team to the "next" value and then switching their teamcolor.
for _,player in pairs(game.Players:GetChildren()) do player.Current_Team.Value = (player.Current_Team.Value + 1)%6 + 1 player.TeamColor = teams[player.Current_Team.Value].TeamColor end
It may not work perfectly, but that's the idea. If you haven't used the modular (%) operator before go ahead and ask about it :)
|
|
|
| Report Abuse |
|
|
|
| 23 Nov 2013 01:01 PM |
| I haven't used the modular (%) operator before... |
|
|
| Report Abuse |
|
|
|
| 23 Nov 2013 01:05 PM |
The modular operator makes it impossible to have numbers over a certain value by returning what's "left over" from dividing.
Example:
0%3 = 0 1%3 = 1 2%3 = 2 3%3 = 0 4%3 = 1
And so on. If you have 6 teams then %6 will make sure you only get 6 values, allowing you to "wrap around" back to team 1 without using pesky logic statements. Notice however that the modular operator starts with '0' not 1, so you will have to add 1 to its value. |
|
|
| Report Abuse |
|
|