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

Datastore doesn't seem to work at all? [closed]

Asked by
QWJKZXF 88
4 years ago

I followed a tutorial on YouTube learning how to write a datastore script, however, when I tested it out, it doesn't seem to work on either studio test mode or through joining the game. API services are turned on and it worked for the tutorial, but not for my script.

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
    -----------------------------------------------------------
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local Cash = Instance.new("IntValue")
    Cash.Name = "Cash"
    Cash.Parent = leaderstats

    local Wins = Instance.new("IntValue")
    Wins.Name = "Wins"
    Wins.Parent = leaderstats
    -----------------------------------------------------------

    local playerUserId = "Player_"..player.UserId

    local data --This turns it into a more global variable.
    --This will load the data, wrap it around a pcall to handle errors if any occur.
    --Everytime the player joins, it will find out if the player has played the game or not or have any data stored.
    local success, errormessage pcall(function()
        local data = myDataStore:GetAsync(playerUserId) --You will assign the GetAsync to this variable.
    end)


    if success then
        --This will set our data to the current amount of cash.
        --After data has been defined from GetAsync, it will not equal to the cash value.
        --This will set the data to its corresponding cash value.
        --Since you are saving data through a table, you may not write Cash.Value = data
        Cash.Value = data.Cash
        Wins.Value = data.Wins
    end

end)

--Save the data, you need the data before it is loaded.
--Since you don't want to save data right when the player joins, you will save it outside of the PlayerAdded event.
game.Players.PlayerRemoving:Connect(function(player)
    --Save the data
    --Get the player's UserId
    local playerUserId = "Player_"..player.UserId
    --Now instead of getting data when loading the game, you set the data when saving it

    local data = {
        Cash = player.leaderstats.Cash.Value; --Take the cash value from player's gui leaderstats.
        Wins = player.leaderstats.Wins.Value;
    }

    --Before setting async, you must wrap it around a pcall to protect the player's data
    local success, errormessage pcall(function()
        myDataStore:SetAsync(playerUserId, data)
    end)

    if success then
        print("Data successfully saved!")
    else
        warn("There was an error in saving the player's data.")
        warn(errormessage)
    end


end)
0
ayy devking pog Spiritlotus 151 — 4y
0
any errors in the console? Pupppy44 671 — 4y
0
Line 55, expected "=". Ziffixture 6913 — 4y
0
-1 This is caused by a typo nc2r 117 — 4y

Closed as Not Constructive by EzraNehemiah_TF2

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

1 answer

Log in to vote
1
Answered by 4 years ago

Maybe you need to add "=" on line 24 and 54

local success, errormessage = pcall(function()
        myDataStore:SetAsync(playerUserId, data)
end)
Ad