Using task.wait() to wait for main script to connect to event first, then exit?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m making a state manager for my game. By far it works, but I’m wondering about the possibility where the connection didn’t happen in time and the ‘quick exit’ gets triggered before the exit connection.

Main script:

function GAMELOGIC:InitializeState()
	if _DEBUG then print("GAMELOGIC: Loading event: Voting") end
	GAMELOGIC.Voting_OnLoad:Fire()
	GAMELOGIC.State = Voting.Start(state)
	
	GAMELOGIC:BindCurrent()
end

Voting state:

function VOTING.Start(...)
	local self = Base.Start(...)	
	setmetatable(self,VOTING)

	self.exitWithTransition = true
	self:CreateExitCondition(KW.ExitConditions.playerrequirement,2,KW.GameState.menu)

	return self
end

the base game state script:

function BaseGameState:CreateExitCondition(exitCondition,conditionParameter,exitToState)
	--
	local subExitEvent = Instance.new("BindableEvent")
	subExitEvent.Name = string.format("%s:%s => %s",self.gameState,exitCondition,exitToState)
	subExitEvent.Parent = RS.Bindables.Communication.ExitEvents
	self.exitConditions[exitCondition] = exitToState
	
	if exitCondition == KW.ExitConditions.playerrequirement then
		if _DEBUG then print("GAMESTATE: Exit condition created for playerrequirement") end
		self.sessionMin = conditionParameter
		self._sessionChangedEvent = subExitEvent
		self._sessionConnection = RS.Bindables.Communication.SessionChanged.Event:Connect(function(nSessions)
			if nSessions <= self.sessionMin then self:TriggerExitCondition(exitCondition) end
		end)

		-- initial check first
		local initialSessions = RS.Bindables.Communication.GetSessions:Invoke()
		if _DEBUG then print("GAMESTATE: initial sessions is "..tostring(initialSessions)) end

		-- delays the quick exit to be after a frame first before execution
		-- (lets gamelogic bind everything successfully first)
		local function quickExit()
			task.wait()
			if initialSessions <= self.sessionMin then self:TriggerExitCondition(exitCondition) end
		end
		task.spawn(quickExit)
		return
	end
end

this works, although i’m not sure if the method is reliable or not. Please let me know and thx :smiley: :smiley: :smiley:

Yeah, that isn’t very reliable. It should work most of the times, but that isn’t fail proof. What I would do is use Signals (or BindableEvents) that are fired when the game is finished binding. Then, instead of task.wait(), you can check if it’s binded or not and if not, wait for that Signal/event to fire.

Code example:

--//Main script
self.finishedLoading = true
BindableEvent:Fire()

--//Other script
if not self.finishedLoading then
    BindableEvent.Event:Wait()
end

--//Main code

That’s awesome, thank you so much for this

1 Like

Some people also prefer Signal over BindableEvents for performance reasons but it’s your choice in the end if you want the added complexity for better performance: Lua Signal Class Comparison & Optimal `GoodSignal` Class

1 Like

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