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 |
|
|
|
| 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
|
  |
| 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 |
|
|
|
| 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
|
  |
| 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 |
|
|