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 » Scripters
Home Search
 

Re: Function Returns

Previous Thread :: Next Thread 
inception101 is not online. inception101
Joined: 19 Dec 2010
Total Posts: 738
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
ExpensiveGaming is not online. ExpensiveGaming
Joined: 01 Apr 2013
Total Posts: 783
24 Mar 2016 10:23 PM
print(test())

#code print("lol im batman")
Report Abuse
me2kool2talk2u is not online. me2kool2talk2u
Joined: 19 Feb 2007
Total Posts: 2914
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
ExpensiveGaming is not online. ExpensiveGaming
Joined: 01 Apr 2013
Total Posts: 783
24 Mar 2016 10:23 PM
local x = print(test())

It returns 'x'

#code print("lol im batman")
Report Abuse
Mister_Manakin is not online. Mister_Manakin
Joined: 16 Sep 2009
Total Posts: 1935
24 Mar 2016 10:25 PM
local x = Test();
print(x);
Report Abuse
inception101 is not online. inception101
Joined: 19 Dec 2010
Total Posts: 738
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
Mister_Manakin is not online. Mister_Manakin
Joined: 16 Sep 2009
Total Posts: 1935
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
me2kool2talk2u is not online. me2kool2talk2u
Joined: 19 Feb 2007
Total Posts: 2914
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
inception101 is not online. inception101
Joined: 19 Dec 2010
Total Posts: 738
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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