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?
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))
For some reason the third function is not saving in time before the player leaves, and the data is reset when the player joins the game. Is it because it’s the last function, which means it saves last? Aren’t coroutines supposed to run at the same time though?
I believe you can only connect a single function to the event, so you have to either make the player removing event for all 3 of them or just run them from 1 function
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
function SaveAll(player)
coroutine.wrap(Save)(player)
coroutine.wrap(Save2)(player)
coroutine.wrap(Save3)(player)
end
game.Players.PlayerRemoving:Connect(SaveAll)
Although not sure how much it’ll help to save all the data, cna’t you just use a single datastore and save a table of stuff instead of 3 separate datastores?
Putting all the coroutines in a SaveAll function still didn’t work for me, the third coroutine didn’t fire in time before the client was removed. Any other ideas?