In my code, I’m using the coroutine function to initiate 3 functions to run at the same time. Each of these 3 functions just sets an async to 3 different datastores. Since there isn’t a lot of time from when the player leaves and when all of their leaderstats, playergui, etc. gets deleted, I need these functions to run at the exact same time, and not one after another. So, I did some research on coroutines and here is what I came up with. Will all 3 functions run at the exact same time when the player leaves the game? No! The third function, which is the last coroutine.wrap called in the game.Players.PlayerRemoving function, is not firing in time before the client is removed from the game. If a coroutine can’t fire all 3 functions at the same time, then what is the point of using coroutines and not just placing functions one after another? Here is my code below.
function Save(player)
local ID = currencyName.."-"..player.UserId
DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end
function Save2(player)
local variable = player.PlayerGui.QuestGui.Frame.MainFrame
LevelQuestDatastore:SetAsync(player.UserId, variable.Quest2.CoinAmount.Text)
end
function Save3(player)
local variable = player.PlayerGui.QuestGui.Frame.MainFrame
CoinQuestDatastore:SetAsync(player.UserId, variable.Quest1.CoinAmount.Text)
end
game.Players.PlayerRemoving:Connect(coroutine.wrap(Save), coroutine.wrap(Save2), coroutine.wrap(Save3))