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: Issue with math.random

Previous Thread :: Next Thread 
theaIone is not online. theaIone
Joined: 03 Jul 2017
Total Posts: 13
19 Jul 2017 03:38 AM
If I do the entire script without the while loop it works perfectly fine, but if I include the while loop it breaks, and I don't know why it isn't working or how to fix it.

------------

b = script.Parent

c = math.random(10)

while c < 11 do
c = math.random(10)
wait(0.01)
end

function onTouch(plr)
local a = plr.Parent.Name

if c > 5 then
game.Players[a].Character:MoveTo(Vector3.new(-123, 9.5, 108))
end

if c <= 5 then
game.Players[a].Character:MoveTo(Vector3.new(-135, 9.5, 108))
end

end

b.Touched:connect(onTouch)


Report Abuse
Proxinable is not online. Proxinable
Joined: 12 Sep 2011
Total Posts: 395
19 Jul 2017 03:47 AM
How does it "break"? (include output)

Also, you can't wait for less than 1/30th of a second (0.033333..) which is what wait() does.
Report Abuse
gum444 is not online. gum444
Joined: 06 May 2011
Total Posts: 549
19 Jul 2017 06:39 AM
well it looks like here you state a variable once:

c = math.random(10)

and then the while loop checks if it's below 10:

while c < 11 do
c = math.random(10)
wait(0.01)
end

it will always be below 10. I dont know what exactly your intent is but you are generating a new variable every 0.03 seconds (like the guy above me said, wait() is a better option)
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
19 Jul 2017 06:46 AM
You need to put infinite loops at the bottom or they will prevent anything else from running. Lua runs in a sequential order (Procedural, if you want the technical paradigm name)
What you put at the top runs first. An infinite loop will keep running, and nothing will run after it. Events are an exception, because events run on a separate thread. (Not really a true thread, but I suspect you don't care either way at this moment)
So, if you want to create a second 'thread' so you don't need to put your loop at the bottom, you can use spawn:
'''Lua
spawn(function()
while c < 11 do
c = math.random(10)
wait(0.01)
end
end)
'''


Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
19 Jul 2017 07:18 AM
Don't use nonsense conditionals.

If you want to run something forever use "while true do".
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