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

How do I use Datastore to make a saving hat inventory?

Asked by 9 years ago

I read up on the wiki on how to make Datastores but I am still not quite sure on how to make it and incorporate it into my GUI. What I want to do is save a variable in datastore that checks if the user has bought the hat (have it a boolean set at true/false) Help please?

EDIT: Would I want to use Datastore or would I use Data Persistance instead? I heard Data persistance is easier to use

1 answer

Log in to vote
0
Answered by
LostPast 253 Moderation Voter
9 years ago

Use something like this:

local DSS = game:GetService("DataStoreService")
local datastore = DSS:GetDataStore("GeneralSaveData", "Players")

function generateDataKey(player)
    local ret = "uid_" .. player.userId
    return ret
end

function generateDataTable(player)
    local dataTable = {
        Points = player.leaderstats.Points.Value,
        Wins = player.leaderstats.Wins.Value
    }
    return dataTable
end

function saveDataForPlayer(player)
    local key = generateDataKey(player)
    local data = generateDataTable(player)
    datastore:SetAsync(key, data)
end

game.Players.PlayerAdded:connect(function(player)
player:WaitForDataReady()
end)

game.Players.PlayerRemoving:connect(function(player)
player:WaitForDataReady()
end)

game.OnClose = function()
    wait(30)
end

You may have to edit it for your purposes. Hope this helps :)

0
Thanks! Am I able to create a new variable not from the leaderboard in the DataStore? Redinosaur 0 — 9y
0
yah you can create a seperate variable. LostPast 253 — 9y
0
you don't need to wait for data to be ready FutureWebsiteOwner 270 — 9y
0
it just helps to make sure data is ready. but yes you don't really need it. it is just an extra protection. LostPast 253 — 9y
Ad

Answer this question