Help with stopping functions

Hello developers! So, I made this already “kinda” working script for my Electronic Marshall System. However, the functions kinda overlay each other… By that I mean: Example of what is happening. Basically, I need to know how to “stop” other functions from running after 1 other starts. By that, I mean for example: I start the “SC_DEP” function, then the “YellowFlagDEP” function. I know that I can use return but I have no idea where and how. Also, I use the repeat until false which won’t really be a big problem after other functions stop, I belive. So yeah, If anyone could just tell me how to stop every other function after starting a new one. (By the way you start functions by chatting a specific message)

My code:

-- SETTINGS

local GreenFlag = "Green_Flag"; -- Message that sets the flag to Green.
local YellowFlag = "Yellow_Flag"; -- Message that sets the flag to Yellow.
local RedFlag = "Red_Flag";  -- Message that sets the flag to Red.
local VirtualSafetyCar = "VSC";  -- Message that sets the screen to VSC.
local SafetyCar = "SC";  -- Message that sets the screen to SC.

-- LOCALS

stop = false
local PlayersService = game:GetService("Players");

-- FUNCTIONS
local function GreenFlagDEP()
	script.Parent.Screen.VSC.Enabled = false
	script.Parent.Screen.SC.Enabled = false
	script.Parent.Screen.BrickColor = BrickColor.new("Lime green")
end;

local function YellowFlagDEP()
	script.Parent.Screen.VSC.Enabled = false
	script.Parent.Screen.SC.Enabled = false
	repeat
	wait(0.35)
	script.Parent.Screen.BrickColor = BrickColor.new("Bright yellow")
	wait(0.35)
	script.Parent.Screen.BrickColor = BrickColor.new("Really black")
	until false
end;

local function RedFlagDEP()
	script.Parent.Screen.VSC.Enabled = false
	script.Parent.Screen.SC.Enabled = false
	repeat
	wait(0.35)
	script.Parent.Screen.BrickColor = BrickColor.new("Really red")
	wait(0.35)
	script.Parent.Screen.BrickColor = BrickColor.new("Really black")
	until false
end;

local function VSC_DEP()
	script.Parent.Screen.BrickColor = BrickColor.new("Really black")
	script.Parent.Screen.VSC.Enabled = true
	script.Parent.Screen.SC.Enabled = false
	repeat
		wait(0.35)
		script.Parent.Screen.Orange.Transparency = 0.35
		wait(0.35)
		script.Parent.Screen.Orange.Transparency = 1
	until false
end;

local function SC_DEP()
	script.Parent.Screen.BrickColor = BrickColor.new("Really black")
	script.Parent.Screen.VSC.Enabled = false
	script.Parent.Screen.SC.Enabled = true
	repeat
		wait(0.35)
		script.Parent.Screen.Orange.Transparency = 0.35
		wait(0.35)
		script.Parent.Screen.Orange.Transparency = 1
	until false
end;
--

PlayersService.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if string.find(string.lower(Message), string.lower(GreenFlag)) then
			print("GreenFlag")
			GreenFlagDEP()
		elseif string.find(string.lower(Message), string.lower(YellowFlag)) then
			print("YellowFlag")
			YellowFlagDEP()
		elseif string.find(string.lower(Message), string.lower(RedFlag)) then
			print("RedFlag")
			RedFlagDEP()
		elseif string.find(string.lower(Message), string.lower(VirtualSafetyCar)) then
			print("VSC")
			VSC_DEP()
		elseif string.find(string.lower(Message), string.lower(SafetyCar)) then
			SC_DEP()
			print("SC")
		end;
	end);
end);

If anything is unclear, please let me know.

1 Like

try coroutines

You could do

task.spawn(GreenFlagDEP)

and repeat that for the others

Do you mean something like this?

PlayersService.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if string.find(string.lower(Message), string.lower(GreenFlag)) then
			print("GreenFlag")
			task.spawn(GreenFlagDEP)
		elseif string.find(string.lower(Message), string.lower(YellowFlag)) then
			print("YellowFlag")
			task.spawn(YellowFlagDEP)
		elseif string.find(string.lower(Message), string.lower(RedFlag)) then
			print("RedFlag")
			task.spawn(RedFlagDEP)
		elseif string.find(string.lower(Message), string.lower(VirtualSafetyCar)) then
			print("VSC")
			task.spawn(VSC_DEP)
		elseif string.find(string.lower(Message), string.lower(SafetyCar)) then
			print("SC")
			task.spawn(SC_DEP)
		end;
	end);
end);

Edit: It still doesn’t work.

Sorry, I misread what you wrote. I still don’t quite get what you mean, you shouldn’t be able to trigger any other functions than the ones that matches what you type. If you mean to disable it after one of those 5 functions is called, you could use a variable or attribute and set it to true. Then do a check and if that variable/attribute is true, then return.

No, problem. I actually found another way to do this, I just had to split the script into 2. Thanks for your help tho.

1 Like