Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Defined variable won't disable another script in Workspace. Any help?

Asked by
uhjos_h 19
4 years ago
Edited 4 years ago

I have tried many times to make this work, and still can not get this correct, any help would be appreciated.

01local boat = script.Parent
02local CanTeleport = false
03local teleportscript = game.Workspace.Teleports.TeleportPad1.Teleport
04while true do
05    wait(10)
06    for i = 1,300 do
07        boat:TranslateBy(Vector3.new(1,0,0))
08        wait()
09    end
10    for i = 1,300 do
11        boat:TranslateBy(Vector3.new(-1,0,0))
12        wait()
13    end
14end
15 
View all 27 lines...
0
If you want to run two while loops concurrently, you should look at coroutines as only one (the first one) will run. Secondly even if both loops were running, the if condition is only checked once so changes to CanTeleport won't be sensed. radiant_Light203 1166 — 4y
0
Yeah, you should coroutine.wrap() your loops if you want to check for that if statement while it's running. (And preferably put it in the loop if you want to consciously check it). pwx 1581 — 4y
0
Also you should know that wait()'s duration can vary wildly. If this is supposed to take a spesific amount of time you should provide a time period. Benbebop 1049 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

this may fix it

01local boat = script.Parent
02local CanTeleport = false
03local teleportscript = game.Workspace.Teleports.TeleportPad1.Teleport
04 
05local function BoatMove()
06    while true do
07        wait(10)
08        for i = 1,300 do
09            boat:TranslateBy(Vector3.new(1,0,0))
10            wait()
11        end
12        for i = 1,300 do
13            boat:TranslateBy(Vector3.new(-1,0,0))
14            wait()
15        end
View all 41 lines...

Basically what went wrong was the code was not able to advance due to the while loops not being in a coroutine or spawn. I went with spawn() for this, but you could also use coroutine.wrap(), both yielding pretty much the same result. Also, your if statement at the end of your code needed to be changed to be in a while loop. This way should work, but infinitely looping isnt always the best way to do things. If I were you, I would look into other options such as GetProperyChangedSignal(). If anything here doesnt work, or you need help, feel free to ask

Ad

Answer this question