Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

3 comments

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.

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

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.

1 answer

✓ Accepted answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.