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
3 years ago
Edited 3 years ago

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

local boat = script.Parent
local CanTeleport = false
local teleportscript = game.Workspace.Teleports.TeleportPad1.Teleport
while true do
    wait(10)
    for i = 1,300 do
        boat:TranslateBy(Vector3.new(1,0,0))
        wait()
    end
    for i = 1,300 do
        boat:TranslateBy(Vector3.new(-1,0,0))
        wait()
    end
end

while true do
    wait (10)
     CanTeleport = true
    wait (10)
     CanTeleport = false

end

if CanTeleport == false then teleportscript.Disabled = true
    if CanTeleport == true then teleportscript.Disabled = false
    end
end
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 — 3y
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 — 3y
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 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

this may fix it

local boat = script.Parent
local CanTeleport = false
local teleportscript = game.Workspace.Teleports.TeleportPad1.Teleport

local function BoatMove()
    while true do
        wait(10)
        for i = 1,300 do
            boat:TranslateBy(Vector3.new(1,0,0))
            wait()
        end
        for i = 1,300 do
            boat:TranslateBy(Vector3.new(-1,0,0))
            wait()
        end
    end
end

local function CanTeleportToggle()
    while true do
        CanTeleport = true
        wait (10)
        CanTeleport = false
        wait (10)
    end
end

local function CanTeleportCheck()
    while true do
        if CanTeleport == false then 
            teleportscript.Disabled = true
        elseif CanTeleport == true then 
            teleportscript.Disabled = false
        end
        wait()
    end
end

spawn(BoatMove)
spawn(CanTeleportToggle)
spawn(CanTeleportCheck)

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