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: Functions In A Table?

Previous Thread :: Next Thread 
PurelyRandom is not online. PurelyRandom
Joined: 05 Mar 2011
Total Posts: 36763
16 May 2014 04:27 PM
I'm trying to get it run a random function from a table, but it always returns nil.


function stringamount()
repeat
justrandom = math.random(25)
until justrandom <= 5

astring = ("String x"..justrandom)
end

function arrowamount()
repeat
random2 = math.random(25)
until random2 <= 10

arrow = ("Arrow x"..random2)
end

function leatherchestplate()
random3 = math.random(100)

if random3 >= 1 and random3 <= 60 then
leatherchestplate = "Torn Leather Chestplate"

elseif random3 >= 61 and random3 <= 75 then
leatherchestplate = "Scratched Leather Chestplate"

elseif random3 >= 76 and random3 <= 90 then
leatherchestplate = "Worn Leather Chestplate"

elseif random3 >= 91 and random3 <= 97 then
leatherchestplate = "Leather Chestplate"

else
leatherchestplate = "Pristine Leather Chestplate"
end
end

function money()
random4 = math.random(100)

if random4 >= 1 and random4 <= 70 then
random5 = math.random(20)
money = ("Copper Coins x"..random5)

elseif random4 >= 71 and random4 <= 85 then
random6 = math.random(15)
money = ("Silver Coins x"..random6)

elseif random4 >= 86 and random4 <= 95 then
random7 = math.random(5)
money = ("Gold Coins x"..random7)
else
random8 = math.random(2)
money = ("Platinum Coins x"..random8)
end
end



loot = {
stringamount(),
arrowamount(),
leatherchestplate(),
money(),
}

randomloot = math.random(4)
chosenloot = loot[randomloot]
print(chosenloot)
Report Abuse
PurelyRandom is not online. PurelyRandom
Joined: 05 Mar 2011
Total Posts: 36763
16 May 2014 04:34 PM
B1
Report Abuse
cooldude999921 is not online. cooldude999921
Joined: 13 Aug 2011
Total Posts: 3702
16 May 2014 04:37 PM
randomloot = math.random(4)
chosenloot = loot[randomloot]


umm...


Report Abuse
PurelyRandom is not online. PurelyRandom
Joined: 05 Mar 2011
Total Posts: 36763
16 May 2014 04:38 PM
Yeah I kinda doubted that part too.

This guy told me about that: http://www.roblox.com/Forum/ShowPost.aspx?PostID=134372416


I guess he was wrong.
Report Abuse
islandmaker2012 is not online. islandmaker2012
Joined: 07 Nov 2012
Total Posts: 9327
16 May 2014 04:40 PM
loot = {
stringamount,
arrowamount,
leatherchestplate,
money,
}
Report Abuse
PurelyRandom is not online. PurelyRandom
Joined: 05 Mar 2011
Total Posts: 36763
16 May 2014 04:43 PM
Oh I wouldn't put the parentheses? Why? :l
Report Abuse
PurelyRandom is not online. PurelyRandom
Joined: 05 Mar 2011
Total Posts: 36763
16 May 2014 05:01 PM
Nevermind, it's working now, I fixed it.

However, there's a new problem. For math.random, how do I get it to only print 1 outcome?

(see comment in script for more explanation)

-- loot variables and functions --
function stringamount()
repeat
justrandom = math.random(25)
until justrandom <= 5

astring = ("String x"..justrandom)
print(astring)
end

function arrowamount()
repeat
random2 = math.random(25)
until random2 <= 10

arrow = ("Arrow x"..random2)
print(arrow)
end

function leatherchestplate()
random3 = math.random(100)

if random3 >= 1 and random3 <= 60 then
leatherchestplate = "Torn Leather Chestplate"

elseif random3 >= 61 and random3 <= 75 then
leatherchestplate = "Scratched Leather Chestplate"

elseif random3 >= 76 and random3 <= 90 then
leatherchestplate = "Worn Leather Chestplate"

elseif random3 >= 91 and random3 <= 97 then
leatherchestplate = "Leather Chestplate"

else
leatherchestplate = "Pristine Leather Chestplate"
end
print(leatherchestplate)
end

function money()
random4 = math.random(100)

if random4 >= 1 and random4 <= 70 then
random5 = math.random(20)
money = ("Copper Coins x"..random5)

elseif random4 >= 71 and random4 <= 85 then
random6 = math.random(15)
money = ("Silver Coins x"..random6)

elseif random4 >= 86 and random4 <= 95 then
random7 = math.random(5)
money = ("Gold Coins x"..random7)
else
random8 = math.random(2)
money = ("Platinum Coins x"..random8)
end
print(money)
end



loot = {
stringamount(),
arrowamount(),
money(),
leatherchestplate()
}

randomloot = math.random(4)
chosenloot = loot[randomloot]
print(chosenloot) -- This runs all 4 functions because chosenloot is set to randomloot, which runs the math.random again. How would I get the math.random to run once and stop?
Report Abuse
PurelyRandom is not online. PurelyRandom
Joined: 05 Mar 2011
Total Posts: 36763
16 May 2014 05:07 PM
B2
Report Abuse
PurelyRandom is not online. PurelyRandom
Joined: 05 Mar 2011
Total Posts: 36763
16 May 2014 05:16 PM
B3
Report Abuse
xXxMoNkEyMaNxXx is not online. xXxMoNkEyMaNxXx
Joined: 03 Oct 2008
Total Posts: 3120
16 May 2014 05:20 PM
loot = { --Pass the functions, not their returns
stringamount,
arrowamount,
money,
leatherchestplate
}

chosenloot = loot[math.random(#loot)]() --Call the function after it's chosen


Functions are a value type, and can be used as values, and you can call them whenever you want.
Report Abuse
PurelyRandom is not online. PurelyRandom
Joined: 05 Mar 2011
Total Posts: 36763
16 May 2014 05:22 PM
Thanks.

I do have a quick question though:

What does the # do/mean in #loot?
Report Abuse
nacker is online. nacker
Joined: 06 Oct 2009
Total Posts: 6034
16 May 2014 05:23 PM
this is how I would do it, see if you can add this to your script
untested so tell me if it does not work

functions = {"f1","f2"} -- change the name of these to your functions
random = math.random(1, 2) -- change 2 to how many functions you have

[functions[random]]()



Report Abuse
PurelyRandom is not online. PurelyRandom
Joined: 05 Mar 2011
Total Posts: 36763
16 May 2014 05:39 PM
Awesome.

And I learned what the # operator does so disregard that question.

However, in the case that I wanted to change the number of times the script runs that math.random, how would I do that?
Report Abuse
PurelyRandom is not online. PurelyRandom
Joined: 05 Mar 2011
Total Posts: 36763
16 May 2014 05:56 PM
B4
Report Abuse
xXxMoNkEyMaNxXx is not online. xXxMoNkEyMaNxXx
Joined: 03 Oct 2008
Total Posts: 3120
16 May 2014 06:08 PM
Simply run it more than once...
Report Abuse
PurelyRandom is not online. PurelyRandom
Joined: 05 Mar 2011
Total Posts: 36763
16 May 2014 06:20 PM
That's what I was going to do, but I think I asked the wrong question.

Is there a more efficient way?
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