Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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 :)

4 comments

0

Thanks! Am I able to create a new variable not from the leaderboard in the DataStore?

0

yah you can create a seperate variable.

0

you don't need to wait for data to be ready

0

it just helps to make sure data is ready. but yes you don't really need it. it is just an extra protection.

This discussion is preserved as it was. New answers, comments, and votes are closed.