My datastore works perfectly fine in game, saving multiple values inside of a table into a datastore. But, once I hop into my game on Roblox, the datastore doesn’t work, the currency value is not saved from my last playing session, and some of the GUI text values that were saved as strings in my table were not saved also. Anybody know why datastores only work in Studio but not in game?
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
local currency = Instance.new("IntValue")
currency.Name = currencyName
currency.Parent = folder
local ID = currencyName.."-"..player.UserId
local playerKey = "Player_" .. player.UserId
local savedData = nil
pcall(function()
savedData = DataStore:GetAsync(ID)
end)
local QuestGui = player.PlayerGui:WaitForChild("QuestGui")
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)
function Save(player)
local ID = currencyName.."-"..player.UserId
local generaldatatable = {player.leaderstats[currencyName].Value, player.PlayerGui:WaitForChild("QuestGui").Frame.MainFrame.Quest1.CoinAmount.Text, player.PlayerGui:WaitForChild("QuestGui").Frame.MainFrame.Quest2.CoinAmount.Text}
DataStore:SetAsync(ID,generaldatatable)
end
game.Players.PlayerRemoving:Connect(function(player)
local ID = currencyName.."-"..player.UserId
local generaldatatable = {player.leaderstats[currencyName].Value, player.PlayerGui:WaitForChild("QuestGui").Frame.MainFrame.Quest1.CoinAmount.Text, player.PlayerGui:WaitForChild("QuestGui").Frame.MainFrame.Quest2.CoinAmount.Text}
DataStore:SetAsync(ID,generaldatatable)
end)
game:BindToClose(function()
-- When game is ready to shutdown
for i, v in pairs(game.Players:GetPlayers()) do
Save(v)
end
wait(5)
end)