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 » Scripters
Home Search
 

Re: A more concise code for this?

Previous Thread :: Next Thread 
Wittiest is not online. Wittiest
Joined: 02 Mar 2009
Total Posts: 9665
13 Feb 2016 02:12 AM
The following code is functional, but I am a noob at efficiently. I'd like for my code to be more readable and concise. Could anyone show me a better way to do this?
-------------
Team1 = "Crimson"
Team2 = "Cyan"

game.Players.PlayerAdded:connect(function(plr)
local num = game.Players.NumPlayers
local rand = math.random(1,2)
if num < 2 then
if rand == 1 then
plr.TeamColor = game.Teams[Team1].TeamColor
else
plr.TeamColor = game.Teams[Team2].TeamColor
end
else
local var = ''
for _,v in pairs(game.Players:GetChildren()) do
if v.Name ~= plr.Name then
var = v.TeamColor
end
end
if var == Team1 then
plr.TeamColor = game.Teams[Team2].TeamColor
else
plr.TeamColor = game.Teams[Team1].TeamColor
end
end
end)
Report Abuse
AdvancedObjects is not online. AdvancedObjects
Joined: 11 Dec 2012
Total Posts: 3354
13 Feb 2016 02:14 AM
local num = #game.Players.NumPlayers*



Report Abuse
AdvancedObjects is not online. AdvancedObjects
Joined: 11 Dec 2012
Total Posts: 3354
13 Feb 2016 02:14 AM
Thought it was local num = game.Players:GetChildren() lmao. My bad.



Report Abuse
Wittiest is not online. Wittiest
Joined: 02 Mar 2009
Total Posts: 9665
13 Feb 2016 02:20 AM
Would really love some tips on making this code more concise. It is junky and nooby like me
Report Abuse
AdvancedObjects is not online. AdvancedObjects
Joined: 11 Dec 2012
Total Posts: 3354
13 Feb 2016 02:21 AM
What are you trying to accomplish via this script? As I can't be bothered going through it and understanding it, lol.



Report Abuse
Wittiest is not online. Wittiest
Joined: 02 Mar 2009
Total Posts: 9665
13 Feb 2016 02:23 AM
My server will only have two players. I want them randomly assigned to team 1 or team 2, but obviously I need one of them on Team1 and the other on Team2.
Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
13 Feb 2016 02:24 AM
What are you trying to do? Just shuffle the teams up?
Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
13 Feb 2016 02:26 AM
Oh I see, you actually don't need a script at all

Under game.Teams.TeamName check 'AutoAssignable' and the teams will be balanced for you.

http://wiki.roblox.com/index.php?title=API:Class/Team/AutoAssignable
Report Abuse
Wittiest is not online. Wittiest
Joined: 02 Mar 2009
Total Posts: 9665
13 Feb 2016 02:26 AM
Synonymous to how in this game when a player is teleported to a server they are put as either the white or black chess player:
http://www.roblox.com/games/95041893/Chess

I am trying to do that in scripting and I feel like I am over complicating it
Report Abuse
AdvancedObjects is not online. AdvancedObjects
Joined: 11 Dec 2012
Total Posts: 3354
13 Feb 2016 02:26 AM
function setTeam(player)
local TeamColours = {"Cyan", "Crimson"}
if #game.Players:GetChildren() <= 2 then
local team = TeamColours[math.random(1, #TeamColours)]
player.TeamColor = BrickColor.new(team)
local num = 0
for i, v in pairs (TeamColours) do
num = num + 1
if v == team then
num = 0
table.remove(TeamColours, num)
break
end
end
end
end
for _, player in pairs (game.Players:GetChildren()) do
if player then
setTeam(player)
end
end



Report Abuse
AdvancedObjects is not online. AdvancedObjects
Joined: 11 Dec 2012
Total Posts: 3354
13 Feb 2016 02:31 AM
My mistake: put "local TeamColours = {"Cyan", "Crimson"}" outside of the function.



Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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