It’s a pretty simple task, I just need to save a table into a datastore. The table consists of 3 values. The first value is a leaderstats value, and the other 2 are strings from Gui’s. For some reason, this datastore works perfectly fine when I test it out in Roblox Studio, but it does not work in game on the Roblox website. I think I know where the problem is, but I’m not sure. I suspect that in Roblox Studio, since when you press Stop, the server closes before the client is removed, the script works. But, in game, it’s probably the opposite. So, I narrowed down the problem to the game.Players.PlayerRemoving part in my script. Here it is:
local function Save(player)
local generaldatatable = {}
table.insert(generaldatatable, player.leaderstats["Coins"].Value)
table.insert(generaldatatable, player.PlayerGui:WaitForChild("QuestGui").Frame.MainFrame.Quest1.CoinAmount.Text)
table.insert(generaldatatable, player.PlayerGui:WaitForChild("QuestGui").Frame.MainFrame.Quest2.CoinAmount.Text)
local success, errormsg = pcall(function()
DataStore:SetAsync(player.UserId, generaldatatable)
end)
if success then
print("Data saved successfully")
else
print("Data not saved successfully")
warn(errormsg)
end
end
game:GetService("Players").PlayerRemoving:Connect(Save)
Here is my whole datastore script:
game:GetService("Players").PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
local currency = Instance.new("IntValue")
currency.Name = "Coins"
currency.Parent = folder
local savedData
getdatastore = nil
local success, error = pcall(function()
savedData = DataStore:GetAsync(player.UserId)
return savedData
end)
if success then
if savedData then
local QuestGui = player.PlayerGui:WaitForChild("QuestGui")
print(savedData)
if savedData[1] ~= nil then
currency.Value = savedData[1]
print("currency data loaded")
else
-- New player
currency.Value = 0
print("New player to the game")
end
if savedData[2] ~= nil and savedData[3] ~= nil then
QuestGui.Frame.MainFrame.Quest1.CoinAmount.Text = savedData[2]
QuestGui.Frame.MainFrame.Quest2.CoinAmount.Text = savedData[3]
game.ReplicatedStorage.Events:WaitForChild("QuestDataLoaded"):FireClient(player)
print("data was loaded")
else
QuestGui.Frame.MainFrame.Quest1.CoinAmount.Text = "0/10"
QuestGui.Frame.MainFrame.Quest2.CoinAmount.Text = "0/1"
print("data was not properly loaded")
end
end
else
warn(error)
end
end)
local function Save(player)
local generaldatatable = {}
table.insert(generaldatatable, player.leaderstats["Coins"].Value)
table.insert(generaldatatable, player.PlayerGui:WaitForChild("QuestGui").Frame.MainFrame.Quest1.CoinAmount.Text)
table.insert(generaldatatable, player.PlayerGui:WaitForChild("QuestGui").Frame.MainFrame.Quest2.CoinAmount.Text)
local success, errormsg = pcall(function()
DataStore:SetAsync(player.UserId, generaldatatable)
end)
if success then
print("Data saved successfully")
else
print("Data not saved successfully")
warn(errormsg)
end
end
game:GetService("Players").PlayerRemoving:Connect(Save)
game:BindToClose(function()
-- When game is ready to shutdown
for _,Player in pairs(game.Players:GetPlayers())do
Save(Player)
end
end)