You can write your topic however you want, but you need to answer these questions:
-
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