After loading a game, a GUI is supposed to display the amount of gold that the player has. The amount of gold the player has is stored in the [1] slot in their DataStore. When I try this in Studio it works perfectly, displaying the number. However, in the actual game, the GUI remains blank; it doesn't display the number nor "error." Does anybody know why this may be?
Here is the script for when the player enters the game.
game.Players.PlayerAdded:connect(function(player) --When the player joins the game, this script fires.
wait(1) --To give time for everything to load up.
local DataStore = game:GetService("DataStoreService"):GetDataStore("insert-store-here") --Loads the DataStore (not a real DataStore; don't wanna give out my actual one.)
local text = player.PlayerGui.Tutorial.SaveNumber --The GUI that displays the Gold.
local key = "user-" ..player.UserId.. "-lastSlot"
local savedValues = DataStore:GetAsync(key)
if savedValues ~= nil then
text.Text = savedValues[1] --savedValues[1] is where the gold value is saved. The text is supposed to display the number in savedValues[1]. In Studio, it does this perfectly, but in the actual game, it's blank.
else
text.Text = "error" --Happens if the key doesn't exist. By the way, if you are wondering why I didn't include a statement for making a new key, it's because that is already handled in an outside script. (see the update)
end
end)
Update: There is a separate script used to create and save the Gold value. This is it.
wait(3)
local DataStore = game:GetService("DataStoreService"):GetDataStore("lorStore")
player = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent
script.Parent.MouseButton1Up:connect(function()
local key = "user-" ..player.UserId.. "-lastSlot"
local savedValues = DataStore:GetAsync(key)
if savedValues ~= nil then
savedValues[1] = player.leaderstats.Gold.Value
else
local valuesToSave = {player.leaderstats.Gold.Value, true}
DataStore:SetAsync(key, valuesToSave)
end
end)

1 comment
When I added a line to print what savedValues[1] is, it printed exactly what was in savedValues[1]. If it helps, I've updated my question to include the script for creating and saving the key.