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: Players set into an Array

Previous Thread :: Next Thread 
foxmaster600 is not online. foxmaster600
Joined: 14 Oct 2009
Total Posts: 147
26 Mar 2015 05:54 PM
So the gist of what I'm about to say it that I want to get all the players with a certain "flag" (Afk/Intermission On/off switch kind of thing) to be put into an array

Then I want a piece of data/ a variable that I have set on the player to determine their role when the game I have planned starts.

The only problem is, I have trouble with syntax in Lua, it's very hard to memorize such things, so I don't know how to actually "write it out".

The whole logic of the code is already figured out, but I don't know the syntax for it, so if someone can explain it to me, that'd be great.
Try to only tell me like what figures to use, and the least coding as possible.
The game's suppose to be made by me after all... not the forums.
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
26 Mar 2015 05:57 PM
What language(s) do you know? :D
Report Abuse
foxmaster600 is not online. foxmaster600
Joined: 14 Oct 2009
Total Posts: 147
26 Mar 2015 06:00 PM
I'm not going to help you code anything... just so you know...

I'm only asking for an explanation and definition.

Also, quite a lot.
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
26 Mar 2015 06:03 PM
I was just wanting to make chat. :P
Report Abuse
NotAshley is not online. NotAshley
Joined: 16 Jan 2014
Total Posts: 14257
26 Mar 2015 06:04 PM
--comments look like this in Lua

--arrays are called tables in Lua, they're dynamic and 1 based
local flaggedPlayers = {}

--now we iterate through all players, GetChildren() makes a new table
for i,v in pairs(game.Players:GetChildren())
--i is the index, v is the value
if v:FindFirstChild("NameOfFlag") --assuming the flags are objects
--add the player's name to the table
flaggedPlayers[#flaggedPlayers + 1] = v.Name
end
end
Report Abuse
amanda is not online. amanda
Joined: 21 Nov 2006
Total Posts: 5925
26 Mar 2015 06:06 PM
You were rather vague on what you wanted.

You want players to have multiple properties, some that that the player can change, and some can't, and want to use these in arrays?

do you want all the players in an array, or do you want them split up into new arrays?

specify more
Report Abuse
foxmaster600 is not online. foxmaster600
Joined: 14 Oct 2009
Total Posts: 147
26 Mar 2015 06:14 PM
I just want the players to be divided into an array.


I would have something else correspondent to it, like that variable, to decide what happens.



This is what I want

Pretend that every comma is another array index.

Player, Player, Player, Player (and so on.)

Now this is another array.

Variable, Variable, Variable, Variable

So now that those variables correspond to each point on the other array,
there are scripts that decide how they are working out, and will assign the set roles of my game to those players.

Then a bunch of other stuff happens, clothing, weapons, etc.

And then I want to teleport them into the game.

Basically I just want it to be moldable to the point where the script will keep track of that array through the whole game, if that player leaves or dies
the game scripts will handle it accordingly. ~= null yay.
Report Abuse
NotAshley is not online. NotAshley
Joined: 16 Jan 2014
Total Posts: 14257
26 Mar 2015 06:22 PM
In lua it's nil instead of null. You got the ~= operator correct though.

There's a special kind of table called a liabrary, where you set a key (string) that corresponds to a value. This could do what I'm assuming you want in 1 table instead of 2.

It works like this:


local someLiabrary = {"somekey" = 1, "ok" = false, "asd" = "apple"}

someLiabrary["somekey"] -- equals 1
someLiabrary["ok"] -- equals false
someLiabrary["asd"] -- equals 'apple'
Report Abuse
NotAshley is not online. NotAshley
Joined: 16 Jan 2014
Total Posts: 14257
26 Mar 2015 06:23 PM
I got my terminology wrong, it's called a dictionary not a library.

http://wiki.roblox.com/index.php?title=Table
Report Abuse
NotAshley is not online. NotAshley
Joined: 16 Jan 2014
Total Posts: 14257
26 Mar 2015 06:24 PM
Also the syntax. It should be:

local someLiabrary = {["somekey"] = 1, ["ok"] = false, ["asd"] = "apple"}
Report Abuse
foxmaster600 is not online. foxmaster600
Joined: 14 Oct 2009
Total Posts: 147
26 Mar 2015 06:48 PM
Ok well that works too I guess,

but how will I divide the players into the library?
Report Abuse
NotAshley is not online. NotAshley
Joined: 16 Jan 2014
Total Posts: 14257
26 Mar 2015 06:54 PM
-- 'local' is the way to set variables for the current scope
-- by default it's global, I use local for most things
-- the GetChildren() method will make a new table out of all children

local players = game.Players:GetChildren()


-- now use a for loop to iterate through the new table

for i,v in pairs(players) do
-- in here, v is equal to the loop value, and i is the index
-- you can create conditionals to check the values
-- FindFirstChild() will return true if it finds a matching child
-- if your player values are actual objects in the player, do this

if v:FindFirstChild("NameOfValue") then
-- stuff here
else if v:FindFirstChild("SomeOtherValue") then
-- different stuff here
end

-- etc
end

Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
26 Mar 2015 06:54 PM
-- Something like this?

local Players = {
player1 = {
Job = {Name = "FireFighter",Pay = 10,Hours = 45},
Money = 672,
Clothes = {Hat = {Color = "Pink",Cost = 4,Size = 2},Shirt = {Color = "Blue",Cost = 7,Size = 4},Pants = {Color = "Red",Cost = 9,Size = 7}}
},
}


I script -~ chimmihc
Report Abuse
NotAshley is not online. NotAshley
Joined: 16 Jan 2014
Total Posts: 14257
26 Mar 2015 06:58 PM
Forgot to mention. For the dictionary, create it at the start, like this:



local plrTable = {} -- empty table/dictionary

for _,v in pairs(game.Players:GetChildren()) do
if v:FindFirstChild("Whatever1") then
plrtable[v.Name] = "qwerty"
else if v:FindFirstChild("Whatever2") then
plrtable[v.Name] = "applesauce"
end
end



Now if you use plrtable["foxmaster600"] and you had a value in your player named "Whatever1" when the check happened, it would return "qwerty", and if the value was "Whatever2", it would return "applesauce"
Report Abuse
foxmaster600 is not online. foxmaster600
Joined: 14 Oct 2009
Total Posts: 147
26 Mar 2015 07:22 PM
It'd be most helpful if you can elaborate on what the "pairs" tool is for, also what how "I" and "v" are affected by it.
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
26 Mar 2015 07:25 PM
http://wiki.roblox.com/index.php?title=For_loop#For

http://wiki.roblox.com/index.php?title=Dictionary#Dictionaries

http://wiki.roblox.com/index.php?title=Pairs#pairs


I script -~ chimmihc
Report Abuse
NotAshley is not online. NotAshley
Joined: 16 Jan 2014
Total Posts: 14257
26 Mar 2015 07:39 PM
table = {"Apple", "Pear", "Orange"}

for index, value in pairs(table) do
print(index)
print(value)
end



The above example will iterate through "table" (loop once for each value). In a programming language like Java, a similar loop looks like this:



String[] stuff = {"Apple", "Pear", "Orange"};

for(String x : stuff){
System.out.println(x);
}



The difference with Lua is there's 2 values. The first value ('index' in the first example) is the index number of the bit of the loop. The second value ('value' in the first example) is the value of the loop, just like x in the Java loop. The first example will output the following:

>1
>Apple
>2
>Pear
>3
>Orange
Report Abuse
foxmaster600 is not online. foxmaster600
Joined: 14 Oct 2009
Total Posts: 147
26 Mar 2015 08:17 PM
http://puu.sh/gR7jQ/da2033eddb.png

So what I have here is a SCREENSHOT of part of the main gameplay code of my game...

Parts of it have been censored for insecure security reasons.

The notes I've put are things I want to put it, and markers.

Also, when I said "how are the players organized in the hard code of Roblox in the "Players" item" I meant how exactly could it be SEPERATED

I still don't get most of it...

(and they said Lua would be easy)
Report Abuse
NotAshley is not online. NotAshley
Joined: 16 Jan 2014
Total Posts: 14257
26 Mar 2015 08:25 PM
There's no particular order in the way the players are organized.

You correctly set an empty table/dictionary. The next step is to loop through all players. Since the for loop iterates through a table, and the GetChildren() method makes a new table out of all of an object's children (the objects inside that object), we can use GetChildren() on game.Players to get a table out of all players and iterate through them.

That can be either done like this:



local plrs = game.Players:GetChildren()
for i,v in pairs(plrs) do
-- stuff
end



Or we can shorten it like this:



for i,v in pairs(game.Players:GetChildren()) do
-- stuff
end



Inside the loop, you want to check if a player has a certain "flag". You haven't specified how you're setting this flag, so I'm assuming you have an object (such as a StringValue) inside each player object named after that flag. Let's assume each player either has a flag named "Apple" or a flag named "Pear". To separate the players in the table, you would do this:



local playerTable = {}
for i,v in pairs(game.Players:GetChildren()) do
if v:FindFirstChild("Apple") -- v is the loop value (each player)
playerTable[v.Name] = "Apple" -- player's name is dictionary key
elseif v:FindFirstChild("Pear")
playerTable[v.Name] = "Pear"
end
end



Now if you wanted to separate the players into their own tables based on their flags, you would do something like this:



-- normal tables, not dictionaries
local apples = {}
local pears = {}

for i,v in pairs(playerTable) do
if v == "Apple" then
apples[#apples + 1] = v -- put string in next slot
elseif v == "Pear" then
pears[#pears + 1] = v -- etc
end
end
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