generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Anyone know... sorry title didn't fit...

Previous Thread :: Next Thread 
Bonebreaker7 is not online. Bonebreaker7
Joined: 23 Oct 2010
Total Posts: 2430
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 is not online. 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
Bonebreaker7 is not online. Bonebreaker7
Joined: 23 Oct 2010
Total Posts: 2430
23 Nov 2013 12:38 PM
Thanks, but there are 6 teams. How would I change it so that it would work?
Report Abuse
Greendolph is not online. Greendolph
Joined: 07 May 2008
Total Posts: 2543
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
Bonebreaker7 is not online. Bonebreaker7
Joined: 23 Oct 2010
Total Posts: 2430
23 Nov 2013 12:40 PM
I'll try greendolph's idea.
Report Abuse
Greendolph is not online. Greendolph
Joined: 07 May 2008
Total Posts: 2543
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
Bonebreaker7 is not online. Bonebreaker7
Joined: 23 Oct 2010
Total Posts: 2430
23 Nov 2013 12:49 PM
Do you know how to do that?
Report Abuse
Greendolph is not online. Greendolph
Joined: 07 May 2008
Total Posts: 2543
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
Bonebreaker7 is not online. Bonebreaker7
Joined: 23 Oct 2010
Total Posts: 2430
23 Nov 2013 01:01 PM
I haven't used the modular (%) operator before...
Report Abuse
Greendolph is not online. Greendolph
Joined: 07 May 2008
Total Posts: 2543
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image