Picking a random function and waiting for the function to finish then continue picking a new one

Alright so I have a script here with two functions that will be called randomly but only if there is not an ongoing “event”/function running. 90% off the script works. It picks a random event once and then no more even when the “event” is done running

Script:

local myFunctions = {}
local dummyTable = {}
local EventBoard = workspace.EventBoard
local FiveNineFour = EventBoard.FiveNineFour
local EventLight = workspace.LightFolder.Pin
local OldLightVal = Color3.fromRGB(213, 115, 61)
local ProximityPrompt = workspace.Station1.StationPart.ProximityPrompt
local OngoingEvent = false
local StatusText = EventBoard.StatusText.SurfaceGui.Frame.TextBox.Text

myFunctions.Event1 = function()
	OngoingEvent = true
	FiveNineFour.Status.Color = Color3.new(1, 0, 0)
	EventLight.Color = Color3.new(1, 0, 0)
	ProximityPrompt.Enabled = true
	StatusText = "One or several stations are experiencing outages"
	
end	
	
	--Waiting for event to stop
	ProximityPrompt.TriggerEnded:Connect(function()
		FiveNineFour.Status.Color = Color3.new(0, 1, 0)
		EventLight.Color = OldLightVal
		ProximityPrompt.Enabled = false
	OngoingEvent = false
	end)


myFunctions.Event2 = function()
	print("Just using this for debugging")
end


for index, func in pairs(myFunctions) do -- loops through the functions table
	table.insert(dummyTable, index) -- adds the index to the dummy table
	-- the index would be something like:
	-- myFunctions.no = function() | "no" would be the index
end


local function EventPick()
	while ProximityPrompt.Enabled == false do
		wait(5)
		myFunctions[dummyTable[math.random(1, #dummyTable)]]()
		
	end

end


	EventPick()

As far as I’m aware while loops won’t act again if the condition is false, so I suggest doing:

while true do
	if ProximityPrompt.Enabled == false then
		wait(5)
		myFunctions[dummyTable[math.random(1, #dummyTable)]]()
	else
		wait()
	end
end

And as long as there isn’t anything in the EventPick function, you can put the loop at the end of the script, outside the function.

Wow thank you for helping me.
I am a quite new Scripter and I really learned something from this.

Much love

1 Like

ProximityPrompt:GetPropertyChangedSignal("Enabled")
To optimise the script further.

ProximityPrompt:GetPropertyChangedSignal("Enabled"):Connect(function()
	if ProximityPrompt.Enabled then return end
	--Call a random function.
end)