youssef04
|
  |
| Joined: 22 Jan 2011 |
| Total Posts: 1745 |
|
|
| 18 Apr 2014 05:19 PM |
| I can script. I'd call myself a beginner, I can do the basics. When I see the word "Tables" My eyes pop out, I have no idea how I can use them, please help by posting examples and explaining. |
|
|
| Report Abuse |
|
|
|
| 18 Apr 2014 05:23 PM |
Example:
--maps maps = { Bricktops Meadows Pitgrounds }
map = (math.random(1,#maps))
newMap = game.Lighting:FindFirstChild(tostring(maps[map])):Clone() newMap.Parent = workspace
This works, because you can get something inside a table using 'ExactTableName[Number]' The number indicates which variable. For example, to get Bricktops, it would be maps[1], to get meadows, maps[2], or to get pitgrounds, maps[3]. And this would go so on. You should get it now.
So math random would work. It takes the number of maps, and chooses it randomly. Then, since you can use maps[number] to get the variable, it does this: maps[RandomNumberHereFrom1ThroughHowManyVariables]
This would do this exactly:
>Choose a random map (Even though it isn't a TRUE random number generator) >And parent it to workspace (This would only work if it is the EXACT name of a map in lighting, but string.lower can be used)
And tostring is required, because the variables aren't strings.
Sorry for any bad explanations, etc. This is just how I see it. |
|
|
| Report Abuse |
|
|
Jezbob
|
  |
| Joined: 19 Nov 2008 |
| Total Posts: 764 |
|
|
| 18 Apr 2014 05:24 PM |
Well, I haven't really done much of tables in Lua, however, they are more commonly known as arrays in the wider programming world and I can explain what they are from that.
Arrays/Tables is a way of storing multiple types of any data (So whole numbers, text, true/false values in one LIST).
So for example, in Lua:
array = {"hello", 4, true, false}
^ Is an array with 4 values. Tables are useful because you can append (Add to), remove things and modify things within the table. Each element of the table is assigned an integer value starting at position 0 and counting along so forth.
So, if I put into Lua:
print(array[0]) I would get 'hello' in my output
if I put into Lua:
print(array[3]) I would get the Boolean value false (Boolean means true/false values like 1 or 0 or on of off - there are only ever two states, like a digital signal if you do physics/electronics).
|
|
|
| Report Abuse |
|
|
Jezbob
|
  |
| Joined: 19 Nov 2008 |
| Total Posts: 764 |
|
|
| 18 Apr 2014 05:26 PM |
My bad arrays in lua start at position 1 NOT 0,
so print(array[1]) would return "hello"
Whops... they start at position 0 in python :P |
|
|
| Report Abuse |
|
|
youssef04
|
  |
| Joined: 22 Jan 2011 |
| Total Posts: 1745 |
|
|
| 18 Apr 2014 05:26 PM |
So, if I want to get all of the children of Players, would it be:
PLR = game.Players:GetChildren() -- Everyone is saying that I am getting the Children of a table. How to fix this?
|
|
|
| Report Abuse |
|
|
youssef04
|
  |
| Joined: 22 Jan 2011 |
| Total Posts: 1745 |
|
|
| 18 Apr 2014 05:28 PM |
So would it be: array = {hello, bob, meow, youssef04} print(array[4]) -- Would that print my name? |
|
|
| Report Abuse |
|
|
Jezbob
|
  |
| Joined: 19 Nov 2008 |
| Total Posts: 764 |
|
|
| 18 Apr 2014 05:29 PM |
| Yep, in the output box you'd see your name |
|
|
| Report Abuse |
|
|
|
| 18 Apr 2014 05:30 PM |
Yes.
When life gives you lemons... BURN HIS HOUSE DOWN! >:D |
|
|
| Report Abuse |
|
|
Jezbob
|
  |
| Joined: 19 Nov 2008 |
| Total Posts: 764 |
|
|
| 18 Apr 2014 05:30 PM |
Remember you can call your array anything you want though, so it can be
pigs = {} bob = {}
What's is important is lua recognises you're trying to create an array when you use the squigily brackets. |
|
|
| Report Abuse |
|
|
youssef04
|
  |
| Joined: 22 Jan 2011 |
| Total Posts: 1745 |
|
|
| 18 Apr 2014 05:32 PM |
Okay, so if I did this, would it work?
Numbers = 1, 2, 3, 4 Array = Lol, hello, bob, youssef04 Numbers2 = Numbers (math.random(1, #Numbers)) print(Array[Numbers2]) |
|
|
| Report Abuse |
|
|
youssef04
|
  |
| Joined: 22 Jan 2011 |
| Total Posts: 1745 |
|
|
| 18 Apr 2014 05:32 PM |
Oops, my bad: Numbers = {1, 2, 3, 4} Array = {Lol, hello, bob, youssef04} Numbers2 = Numbers (math.random(1, #Numbers)) print(Array[Numbers2]) |
|
|
| Report Abuse |
|
|
Jezbob
|
  |
| Joined: 19 Nov 2008 |
| Total Posts: 764 |
|
|
| 18 Apr 2014 05:36 PM |
No, I don't think so:
Array = { "Lol", "hello", "bob", "youssef04" } Numbers2 = math.random(1,4) print(Numbers2) print(Array[Numbers2])
Is a simple version of what you want to do I think.
You hadn't put squifily brackets on your array, you didn't put quotation marks around your text/strings! (Remember this :P) |
|
|
| Report Abuse |
|
|
youssef04
|
  |
| Joined: 22 Jan 2011 |
| Total Posts: 1745 |
|
|
| 18 Apr 2014 05:36 PM |
When I done my script, it gave me this output: 23:35:27.497 - Workspace.Script:3: attempt to get length of global 'Numbers' (a number value) What do I do to fix this? |
|
|
| Report Abuse |
|
|
Jezbob
|
  |
| Joined: 19 Nov 2008 |
| Total Posts: 764 |
|
|
| 18 Apr 2014 05:39 PM |
| I'm not sure, but I think it's because you are trying to generate a random number from 1 to an array which doesn't make any sense. You need to generate a random number between fixed points and an array is not a fixed point, it contains multiple values! |
|
|
| Report Abuse |
|
|
youssef04
|
  |
| Joined: 22 Jan 2011 |
| Total Posts: 1745 |
|
|
| 18 Apr 2014 05:41 PM |
Nevermind, yours worked. Now, if I wanted to give a random player a tool. Would it be something like:
while wait() do local PLR = game.Players:GetChildren() -- I kept getting told, that I am getting the Children of a table, how to fix? local PLR2 = (math.random(PLR)) print(PLR2) game.Lighting.Gun:Clone().Parent = PLR2.Backpack game.Lighting.Gun:Clone().Parent = PLR2.StarterGear wait(100) Gun:Remove().Parent end |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 18 Apr 2014 05:41 PM |
| local PLR2 = PLR[math.random(#PLR)] |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 18 Apr 2014 05:42 PM |
'Gun:Remove().Parent' makes no sense |
|
|
| Report Abuse |
|
|
|
| 18 Apr 2014 05:42 PM |
Make a table of everything in workspace
for i,v in pairs(game.Workspace:GetChildren())do v:Destroy() end
then it would remove v, which would be everything in workspace.
I got pecked by TWO chicken's! |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 18 Apr 2014 05:43 PM |
No, it would be the value of said iteration. Basically "table of children of workspace"[i] |
|
|
| Report Abuse |
|
|
|
| 18 Apr 2014 05:43 PM |
local plrs = game.Players:GetPlayers() local picked = plrs[math.random(1,#plrs)] game.Lighting.Gun:Clone().Parent = picked.Backpack game.Lighting.Gun:Clone().Parent = picked.StarterGear wait(100) picked.StarterGear.Gun:Remove() picked.Backpack.Gun:Remove()
When life gives you lemons... BURN HIS HOUSE DOWN! >:D |
|
|
| Report Abuse |
|
|
|
| 18 Apr 2014 05:44 PM |
God, I forgot the generic for loop... Eh, I'm tired. Night! :P
When life gives you lemons... BURN HIS HOUSE DOWN! >:D |
|
|
| Report Abuse |
|
|
youssef04
|
  |
| Joined: 22 Jan 2011 |
| Total Posts: 1745 |
|
|
| 18 Apr 2014 05:45 PM |
| @Sprongo, thanks very much. |
|
|
| Report Abuse |
|
|
youssef04
|
  |
| Joined: 22 Jan 2011 |
| Total Posts: 1745 |
|
|
| 18 Apr 2014 05:56 PM |
Any other things you can do with arrays/tables. What I have learnt so far:
Array = { "Lol", "hello", "bob", "youssef04" } Numbers2 = math.random(1,4) print(Numbers2) print(Array[Numbers2])
AND
while wait() do local plrs = game.Players:GetPlayers() local picked = plrs[math.random(1,#plrs)] local picked2 = plrs[math.random(1,#plrs)] game.Lighting.Gun:Clone().Parent = picked.Backpack game.Lighting.Gun:Clone().Parent = picked.StarterGear game.Lighting.Knife:Clone().Parent = picked2.Backpack game.Lighting.Knife:Clone().Parent = picked2.StarterGear wait(10) picked.StarterGear.Gun:Remove() picked.Backpack.Gun:Remove() picked2.StarterGear.Knife:Remove() picked2.Backpack.Knife:Remove() end
|
|
|
| Report Abuse |
|
|
|
| 18 Apr 2014 06:29 PM |
@COOL Are you an idiot?
You cannot find something in lighting like: maps = { Bricktops Watever }
Fixed version(Strings):
maps = { "Brickstops" "Watever" }
|
|
|
| Report Abuse |
|
|
|
| 19 Apr 2014 08:42 AM |
I'm not an idiot. I used tostring, and it works because I tested it. k |
|
|
| Report Abuse |
|
|