✓ Accepted answer
There is an example on adding 'Hats' to leaderboard. This should work if your game is published to Roblox:
local DSService = game:GetService('DataStoreService'):GetDataStore('Hamburger223232')
game.Players.PlayerAdded:connect(function(plr)
local uniquekey = 'id-'..plr.userId
local leaderstats = Instance.new('IntValue', plr)
local savevalue = Instance.new('IntValue')
leaderstats.Name = 'leaderstats'
savevalue.Parent = leaderstats
savevalue.Name = 'Hats'
local GetSaved = DSService:GetAsync(uniquekey)
if GetSaved then
savevalue.Value = GetSaved[1]
else
local NumbersForSaving = {savevalue.Value}
DSService:SetAsync(uniquekey, NumbersForSaving)
end
end)
game.Players.PlayerRemoving:connect(function(plr)
local uniquekey = 'id-'..plr.userId
local Savetable = {plr.leaderstats.Hats.Value}
DSService:SetAsync(uniquekey, Savetable)
end)
Make sure this is a NORMAL script. You can put it to ServerScriptService or workspace
EDIT: If you get an error like 403: Cannot write to DataStore from studio if API access is not enabled. just go to this website, find your game, click on the game's name (not at Start Place: ..., above), find Enable Studio Access to API Services, check that, click on save and you done!
1 comment
You can't physically save a
Hatinstance in datastore. You would instead save a table of Hat names or unique hat IDs the player owns. When the player join again, you would load in their hats using the same table.