Zkiller11
|
  |
| Joined: 25 Aug 2009 |
| Total Posts: 2914 |
|
|
| 31 Jul 2013 11:18 PM |
| I'm trying to use math.random on a :GetChildren() table (using a for i,v in ipairs loop), and instead of picking one of the children, it loops through the table for as many children as the table has. For example: if I have a table with two items in it, it does math.random two times. If three items, three times. Why is it doing this? I only want it to do it once... |
|
|
| Report Abuse |
|
|
|
| 31 Jul 2013 11:20 PM |
so, say you have 10 players.
are you trying to get 10 random players? |
|
|
| Report Abuse |
|
|
WishNite
|
  |
| Joined: 11 Feb 2009 |
| Total Posts: 15828 |
|
|
| 31 Jul 2013 11:21 PM |
| Post the excerpt of the for loop in the script - I assume that you put the math.random() part within the for loop, however. |
|
|
| Report Abuse |
|
|
|
| 31 Jul 2013 11:22 PM |
local si = {}
par = si math.random(1, #si) |
|
|
| Report Abuse |
|
|
ZachBloxx
|
  |
| Joined: 26 Jun 2013 |
| Total Posts: 2833 |
|
|
| 31 Jul 2013 11:24 PM |
| It's supposed to loop through the values in the table. |
|
|
| Report Abuse |
|
|
Zkiller11
|
  |
| Joined: 25 Aug 2009 |
| Total Posts: 2914 |
|
|
| 31 Jul 2013 11:26 PM |
local debounce2 = false local player = game.Players.LocalPlayer local CurrentPackP = player.Backpack.Primary local sp = sps.Primary sps.Frame2:FindFirstChild("Randomize", true).MouseButton1Down:connect(function() if debounce2 then return end debounce2 = true -- Probably don't need the debounce, but eh. I was testing. if player then if CurrentPackP then CurrentPackP:ClearAllChildren() -- I have to clear it twice because it loops too many times local Primary = game.Lighting.Primary:GetChildren() for i, v in ipairs (Primary) do local v2 = Primary[math.random(1,#Primary)] -- Issue CurrentPackP:ClearAllChildren() local v3 = v2:Clone()
v3.Parent = player.Backpack.Primary sps.Frame2.PrimaryWeapon.Text = v3.Name.. "" if sps.Frame2:FindFirstChild("RandomizeTL", true) then sps.Frame2:FindFirstChild("RandomizeTL", true).Name = "Ready9" end sps.Frame2:FindFirstChild("Randomize", true).Text = "Class Randomized!" wait(2) sps.Frame2:FindFirstChild("Randomize", true).Text = "" debounce2 = false end end end end)
Er... That's roughly it. Take it for granted that everything that needs to be defined is defined. Tell me if the code is too large; I'll try and shrink it some more. |
|
|
| Report Abuse |
|
|
|
| 31 Jul 2013 11:30 PM |
function scrambleTable(tab) local returnTable = {} for i = 1,#tab do table.insert(returnTable,table.remove(tab,math.random(1,#tab))) end return returnTable end
local scrambled = scrableTable(game.Players:GetChildren())
If that is what you are trying to do, it scrambles a table.
Making sure that you have an even amount for you max players, this script will select two random players.
local scrambled = scrableTable(game.Players:GetChildren())
function selectPlayers(tab) for i = 2,#scrambled,2 do local player1 = tab[i] local player2 = tab[i-1] end end
|
|
|
| Report Abuse |
|
|
Zkiller11
|
  |
| Joined: 25 Aug 2009 |
| Total Posts: 2914 |
|
|
| 01 Aug 2013 01:04 PM |
| Er... IAmAwesome777, I didn't quite understand what you did.. Is that all really necessary for what I'm trying to do? That seems somewhat excessive... |
|
|
| Report Abuse |
|
|
Ekkoh
|
  |
| Joined: 22 Oct 2012 |
| Total Posts: 524 |
|
|
| 01 Aug 2013 01:06 PM |
Use this.
local function getRandomPlayer() local plrs = Game:GetService('Players'):GetPlayers(); return plrs[math.random(1, #plrs)] end
local random_player = getRandomPlayer() print(random_player.Name) |
|
|
| Report Abuse |
|
|
Zkiller11
|
  |
| Joined: 25 Aug 2009 |
| Total Posts: 2914 |
|
|
| 01 Aug 2013 02:53 PM |
I don't know how exactly we got to Players, but... What I'm wanting to know is why when I use math.random on a table generated using :GetChildren(), instead of randomly picking a single child, it goes through the entire table. When I use a regular table (TableOfFunStuff = {"Item 1", "Item 2"}) math.random randomly picks one of the items inside of the table. Why doesn't it do this for :GetChildren()? |
|
|
| Report Abuse |
|
|
Zkiller11
|
  |
| Joined: 25 Aug 2009 |
| Total Posts: 2914 |
|
| |
|
|
| 01 Aug 2013 06:26 PM |
Try that
local Primary = game.Lighting.Primary:GetChildren() for i = 1,#Primary do local v2 = Primary[math.random(1,#Primary)] -- Issue CurrentPackP:ClearAllChildren() local v3 = v2:Clone()
v3.Parent = player.Backpack.Primary sps.Frame2.PrimaryWeapon.Text = v3.Name.. "" if sps.Frame2:FindFirstChild("RandomizeTL", true) then sps.Frame2:FindFirstChild("RandomizeTL", true).Name = "Ready9" end sps.Frame2:FindFirstChild("Randomize", true).Text = "Class Randomized!" wait(2) sps.Frame2:FindFirstChild("Randomize", true).Text = "" debounce2 = false end |
|
|
| Report Abuse |
|
|
Zkiller11
|
  |
| Joined: 25 Aug 2009 |
| Total Posts: 2914 |
|
| |
|
Zkiller11
|
  |
| Joined: 25 Aug 2009 |
| Total Posts: 2914 |
|
| |
|
Zkiller11
|
  |
| Joined: 25 Aug 2009 |
| Total Posts: 2914 |
|
| |
|
Zkiller11
|
  |
| Joined: 25 Aug 2009 |
| Total Posts: 2914 |
|
| |
|
|
| 01 Aug 2013 09:34 PM |
let me take the next one for you
Bump |
|
|
| Report Abuse |
|
|
Zkiller11
|
  |
| Joined: 25 Aug 2009 |
| Total Posts: 2914 |
|
|
| 01 Aug 2013 09:34 PM |
| Or you could answer my question |
|
|
| Report Abuse |
|
|
|
| 01 Aug 2013 09:36 PM |
| i would but im not exactly a good scripter )_) |
|
|
| Report Abuse |
|
|
Zkiller11
|
  |
| Joined: 25 Aug 2009 |
| Total Posts: 2914 |
|
| |
|
|
| 01 Aug 2013 10:48 PM |
Wait, so let me get this straight
You want to create a list of items, and each player gets a random item off of that list, but no two people have the same weapons?
Sorry if I'm wrong here, I just quickly read the post. |
|
|
| Report Abuse |
|
|
Zkiller11
|
  |
| Joined: 25 Aug 2009 |
| Total Posts: 2914 |
|
|
| 01 Aug 2013 11:10 PM |
Ah...No.
I want to cycle through a list of items (when a player clicks a TextBox,) and then randomly pick one of those items and clone it. Basically, I'm wanting math.random to do what it does best: pick something at random. |
|
|
| Report Abuse |
|
|
|
| 01 Aug 2013 11:33 PM |
table.sort(tab, function(a, b) return math.random() < 0.5 end)
That will randomise a table. |
|
|
| Report Abuse |
|
|
Zkiller11
|
  |
| Joined: 25 Aug 2009 |
| Total Posts: 2914 |
|
|
| 02 Aug 2013 01:52 PM |
Didn't work.
Output:
14:52:05.453 - Players.Player1.PlayerGui.Create-A-Class.Create-A-Class-Loc:376: attempt to call field 'sort' (a nil value) 14:52:05.457 - Script "Players.Player1.PlayerGui.Create-A-Class.Create-A-Class-Loc", Line 376 14:52:05.459 - stack end 14:52:05.462 - Disconnected event because of exception
local debounce2 = false local player = game.Players.LocalPlayer local CurrentPackP = player.Backpack.Primary local sp = sps.Primary sps.Frame2:FindFirstChild("Randomize", true).MouseButton1Down:connect(function() if debounce2 then return end debounce2 = true if player then if CurrentPackP then CurrentPackP:ClearAllChildren() local Primary = game.Lighting.Primary:GetChildren() for i,v in ipairs (Primary) do local v2 = Primary.sort(tab,function(a,b) return math.random() < 0.5 end) --Error line CurrentPackP:ClearAllChildren() local v3 = v2:Clone() v3.Parent = player.Backpack.Primary sps.Frame2.PrimaryWeapon.Text = v3.Name.. "" if sps.Frame2:FindFirstChild("RandomizeTL", true) then sps.Frame2:FindFirstChild("RandomizeTL", true).Name = "Ready9" end sps.Frame2:FindFirstChild("Randomize", true).Text = "Class Randomized!" wait(2) sps.Frame2:FindFirstChild("Randomize", true).Text = "" debounce2 = false end end end end) |
|
|
| Report Abuse |
|
|
|
| 02 Aug 2013 03:33 PM |
table.sort(Primary, ...
And not Primary = table.sort... |
|
|
| Report Abuse |
|
|