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: Using math.random on :GetChildren()

Previous Thread :: Next Thread 
Zkiller11 is not online. 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
IamAwesome777 is not online. IamAwesome777
Joined: 18 Jul 2011
Total Posts: 2640
31 Jul 2013 11:20 PM
so, say you have 10 players.

are you trying to get 10 random players?
Report Abuse
WishNite is not online. 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
flatline115 is not online. flatline115
Joined: 29 Jul 2013
Total Posts: 7826
31 Jul 2013 11:22 PM
local si = {}

par = si math.random(1, #si)
Report Abuse
ZachBloxx is not online. 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 is not online. 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
IamAwesome777 is not online. IamAwesome777
Joined: 18 Jul 2011
Total Posts: 2640
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 is not online. 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 is not online. 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 is not online. 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 is not online. Zkiller11
Joined: 25 Aug 2009
Total Posts: 2914
01 Aug 2013 06:20 PM
Bump
Report Abuse
CaptainLoony95 is not online. CaptainLoony95
Joined: 20 Feb 2009
Total Posts: 4852
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 is not online. Zkiller11
Joined: 25 Aug 2009
Total Posts: 2914
01 Aug 2013 06:38 PM
@CaptainLoony95:

Nope.
Report Abuse
Zkiller11 is not online. Zkiller11
Joined: 25 Aug 2009
Total Posts: 2914
01 Aug 2013 07:55 PM
Bump
Report Abuse
Zkiller11 is not online. Zkiller11
Joined: 25 Aug 2009
Total Posts: 2914
01 Aug 2013 08:30 PM
Bump.
Report Abuse
Zkiller11 is not online. Zkiller11
Joined: 25 Aug 2009
Total Posts: 2914
01 Aug 2013 09:33 PM
Bump..
Report Abuse
areaquest2 is not online. areaquest2
Joined: 05 Jun 2010
Total Posts: 196
01 Aug 2013 09:34 PM
let me take the next one for you


Bump
Report Abuse
Zkiller11 is not online. Zkiller11
Joined: 25 Aug 2009
Total Posts: 2914
01 Aug 2013 09:34 PM
Or you could answer my question
Report Abuse
areaquest2 is not online. areaquest2
Joined: 05 Jun 2010
Total Posts: 196
01 Aug 2013 09:36 PM
i would but im not exactly a good scripter )_)
Report Abuse
Zkiller11 is not online. Zkiller11
Joined: 25 Aug 2009
Total Posts: 2914
01 Aug 2013 10:23 PM
Bump
Report Abuse
CloneTrooper1019 is not online. CloneTrooper1019
Joined: 19 Jan 2009
Total Posts: 6227
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 is not online. 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
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
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 is not online. 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
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
02 Aug 2013 03:33 PM
table.sort(Primary, ...

And not Primary = table.sort...
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