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: Overlapping function, keeping the values local to function.

Previous Thread :: Next Thread 
opplo is not online. opplo
Joined: 09 Dec 2008
Total Posts: 5320
06 Jan 2015 06:37 PM
How do I keep the rand value local to that specific execution of the function. For example if I clicked once and clicked again, after the 5 seconds it would return "rand" as the value from when I clicked the second time.

Hopefully that makes sense, example of output below.

function click()
rand = math.random(100,200)
print(rand)
wait(5)
print(rand)
end

mouse = game.Players.LocalPlayer:GetMouse()
mouse.Button1Down:connect(click)


Output:
123 -- First Click
150 -- Second Click
150 -- End of first Click
150 -- End of Second Click
Report Abuse
BowtieMod is not online. BowtieMod
Joined: 01 Apr 2013
Total Posts: 804
06 Jan 2015 06:42 PM
local rand
Report Abuse
LucasLua is not online. LucasLua
Joined: 18 Jun 2008
Total Posts: 7386
06 Jan 2015 06:43 PM
Use this:

local rand = math.random(100, 200)

By using the "local" keyword, the variable will only be "visible" within its scope. Since its scope is within the function "click", another run of the function "click" running at the same time as the other "click" function will not be able to see or use this variable.
Report Abuse
opplo is not online. opplo
Joined: 09 Dec 2008
Total Posts: 5320
06 Jan 2015 06:43 PM
Doesn't retain it to that execution of the function. I want it local to that specific execution so when I run the function again it doesn't change it for other ongoing executions.
Report Abuse
opplo is not online. opplo
Joined: 09 Dec 2008
Total Posts: 5320
06 Jan 2015 06:45 PM
Oh sorry it does work, my bad. That's odd i'm sure i tried it before and it didn't work.

Thank you anyway.
Report Abuse
DoAndDoOver is not online. DoAndDoOver
Joined: 06 Sep 2014
Total Posts: 628
06 Jan 2015 06:46 PM
so you want a random number

and then you want to keep that random number?
Report Abuse
DoAndDoOver is not online. DoAndDoOver
Joined: 06 Sep 2014
Total Posts: 628
06 Jan 2015 06:46 PM
oh good, i was like, that should work
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