|
| 24 Mar 2016 10:22 PM |
I want to return a random number from a function using a while loop.
function Test() x = math.random(1,5) return x end
while wait(1) do Test() print(x) -gives error because return is not a global domain end
Now obviously the variable 'x' isn't a global domain and is currently isolated to the above function, but how would I made this work? Thanks!
inception101 trade, development, design |
|
|
| Report Abuse |
|
|
|
| 24 Mar 2016 10:23 PM |
print(test())
#code print("lol im batman") |
|
|
| Report Abuse |
|
|
|
| 24 Mar 2016 10:23 PM |
You're correct about the variable not being global, it is local to the function. Your function Test returns the value of x when it is called, so all you have to do is fire the function:
while wait(1) do print(Test()) end |
|
|
| Report Abuse |
|
|
|
| 24 Mar 2016 10:23 PM |
local x = print(test())
It returns 'x'
#code print("lol im batman") |
|
|
| Report Abuse |
|
|
|
| 24 Mar 2016 10:25 PM |
local x = Test(); print(x); |
|
|
| Report Abuse |
|
|
|
| 24 Mar 2016 10:29 PM |
Well say I have the while function like this:
while wait(1) then if Test() then print(Test()) end end
Wouldn't there be a more efficient way to write this? Because in my circumstance I will need to call hundreds of lines of code multiple times it gets passed down the hierarchy of the loop. Very inefficient...I think.
inception101 trade, development, design |
|
|
| Report Abuse |
|
|
|
| 24 Mar 2016 10:31 PM |
If you really wanna be efficient as in reducing the number of lines just use
while wait(1) do print(math.random(1,5)) end
|
|
|
| Report Abuse |
|
|
|
| 24 Mar 2016 10:32 PM |
Computers are faster than you think. Also more code != more computing power.
Also, if the Test() method is the same as the one you defined earlier the if statement wouldn't be necessary, as it returns an integer, which will always be true in a conditional statement. |
|
|
| Report Abuse |
|
|
|
| 24 Mar 2016 10:34 PM |
@me2kool Alright thanks for the help! Also I just realized all returns are true duh, but the loop in my game more times than less will return false. Again thanks!
inception101 trade, development, design |
|
|
| Report Abuse |
|
|