Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why Does This Script Load DataStore Values in Studio, But Loads Nothing on the Actual Roblox Game?

Asked by
8391ice 91
8 years ago
Edited 8 years ago

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 answer

Log in to vote
0
Answered by 8 years ago

Hey 839Slice!

First off, I suggest you use the IsLoaded() method from the game datamodel to check if the game has loaded or not. It returns a boolean to tell you if the game has loaded instead of the inefficient wait(1) as it may take longer than that for the game to actually load.The link to check it out is right here:

http://wiki.roblox.com/index.php?title=API:Class/DataModel/IsLoaded

This is to ensure you launch your datastore script once the game is truly loaded.

Also, don't worry about specifying what Datastore name you associated with the service. The string is actually not your datastore's identity on Roblox's servers. It's just a way to distinguish the datastore from other datastores within your game.

Nobody else can actually use the GetService() method of the game datamodel to steal your datastore as it is solely independent towards your game. Your game is different from our game datamodel, so we cannot use GetService() anywhere to actually grab your data.

Also, your issue may be of three things:

1) Your key is not a string. Keys must be strings when using them to grab data from a datastore.

2) The data exists, but the specific index doesn't. Something deleted the specified index but not the entirety of the data. This handles through inefficient assignment, checks, or changes to your data structures.

3) Your GUI object's properties are messing up the visual display.

Print out what savedValues[1] is and reply back. Check that it's actually a value.

0
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. 8391ice 91 — 8y
Ad

Answer this question