Best way to create a "callable" loop?

Hello!

I am creating a script to manage lights on a vehicle that I have. I have a GUI that displays the vehicle’s info. Of the many info types, I have 2 blinker icons (right and left) and a hazard icon. The idea is that when a player presses a certain key or clicks on the ImageButton it will light up depending on what they clicked.

I have created 3 “loops” that go something like this:

local function BlinkerR()
	repeat wait() until BlinkerROn == true
	wait(.35)
	Dash.RBlinker.ImageColor3 = Color3.fromRGB(100, 100, 100)
	wait(.35)
	Dash.RBlinker.ImageColor3 = Color3.fromRGB(19, 255, 2)
	BlinkerR()
end

There are two more of those “looping functions” that are very similar and work the same way. (Obviously, they just have different operations). The issue that I have run into is that none of the loops stop and only 1 can be called throughout the entire script.

I have attempted inserting if not BlinkerROn then return end in place of repeat wait() until BlinkerROn == true, but that did not work any better.

Any help or ideas is greatly appreciated.

Spawn it in a separate thread:

task.spawn(loop1)
task.spawn(loop2)
1 Like

If you do something like this, it will stop – the reason the other function(s) wouldn’t work is because it got tied up in the initial recursive loop, by using task.spawn(), you can call that function and the script will continue:

local BlinkerROn = true

local function BlinkerR()
	if BlinkerROn then
		wait(.35)
		Dash.RBlinker.ImageColor3 = Color3.fromRGB(100, 100, 100)
		wait(.35)
		Dash.RBlinker.ImageColor3 = Color3.fromRGB(19, 255, 2)
		BlinkerR()
	end
end

task.spawn(function()
	BlinkerR()
end)

wait(2)
BlinkerROn = false
1 Like

Hey! Thanks for the response and explanation. This makes sense now. I am able to call the functions separately, however, they are not detecting when the variable is set to false. They are still looping forever.

No worries – is there more to your code that may be causing the issue? Where is the variable being set to true / false?

1 Like

The variable is being set above the functions and the functions are being called (now spawned) with InputBegan and MouseButton1Down events.

Example:

local UIS=game:GetService("UserInputService")

local BlinkerROn = false

local function BlinkerR()
	if BlinkerROn then
		wait(.35)
		Dash.RBlinker.ImageColor3 = Color3.fromRGB(100, 100, 100)
		wait(.35)
		Dash.RBlinker.ImageColor3 = Color3.fromRGB(19, 255, 2)
		BlinkerR()
	end
end

UIS.InputBegan:connect(function(Input, GP)
	if not GP then
		if Input.KeyCode==Enum.KeyCode.C then
			if BlinkerLOn then
				BlinkerLOn = false
			elseif BlinkerLOn == false then
				BlinkerLOn = true
				task.spawn(BlinkerL)
			end
		end
	end
end)
Dash:WaitForChild("RBlinker").MouseButton1Down:Connect(function()
	if BlinkerROn then
		BlinkerROn = false
	elseif BlinkerROn == false then
		BlinkerROn = true
		BlinkerR()
	end
end)

Hope this helps!

Change your code to this:

local UIS=game:GetService("UserInputService")

local BlinkerROn = false

local function BlinkerR()
	if BlinkerROn then
		wait(.35)
		Dash.RBlinker.ImageColor3 = Color3.fromRGB(100, 100, 100)
		wait(.35)
		Dash.RBlinker.ImageColor3 = Color3.fromRGB(19, 255, 2)
		BlinkerR()
	end
end

UIS.InputBegan:connect(function(Input, GP)
	if not GP then
		if Input.KeyCode==Enum.KeyCode.C then
			if BlinkerLOn then
				BlinkerLOn = false
			elseif BlinkerLOn == false then
				BlinkerLOn = true
				task.spawn(function()
					BlinkerL()
				end)
			end
		end
	end
end)
Dash:WaitForChild("RBlinker").MouseButton1Down:Connect(function()
	if BlinkerROn then
		BlinkerROn = false
	elseif BlinkerROn == false then
		BlinkerROn = true
		BlinkerR()
	end
end)

Should I change it for the MouseButton1Down function as well?

Oh I didn’t notice that, yes – make sure to change that too.

task.spawn(function()
	BlinkerR()
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.