|
| 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 |
|
|
| |
|
|
| 16 May 2014 04:37 PM |
randomloot = math.random(4) chosenloot = loot[randomloot]
umm...
|
|
|
| Report Abuse |
|
|
|
| 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 |
|
|
|
| 16 May 2014 04:40 PM |
loot = { stringamount, arrowamount, leatherchestplate, money, } |
|
|
| Report Abuse |
|
|
|
| 16 May 2014 04:43 PM |
| Oh I wouldn't put the parentheses? Why? :l |
|
|
| Report Abuse |
|
|
|
| 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 |
|
|
| |
|
| |
|
|
| 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 |
|
|
|
| 16 May 2014 05:22 PM |
Thanks.
I do have a quick question though:
What does the # do/mean in #loot? |
|
|
| Report Abuse |
|
|
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 |
|
|
|
| 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 |
|
|
| |
|
|
| 16 May 2014 06:08 PM |
| Simply run it more than once... |
|
|
| Report Abuse |
|
|
|
| 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 |
|
|