I need help making DataStores work in my game.
I’m trying to add Datastores for the leaderboards in my game and made a script that I thought would work but doesn’t.
local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:connect(function(plr)
wait()
local plrkey = "id_"..plr.userid
local save1 = plr.leaderstats.Archives
local save2 = plr.leaderstats.Movies
local save3 = plr.leaderstats.Spaces
local GetSaved = ds:GetAsync(plrkey)
if GetSaved then
save1.Value = GetSaved[1]
save2.Value = GetSaved[2]
save3.Value = GetSaved[3]
else
local NumberForSaving = {save1.Value, save2.Value, save3.Value}
ds:GetAsync(plrkey, NumberForSaving)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
ds:SetAsync("id_"..plr.userid, {plr.leaderstats.Archives.Value, plr.leaderstats.Movies.Value, plr.leaderstats.Spaces.Value})
end)
Here is my leaderboard script which does work but here it is if it may be important.
-- Creates a leaderboard that shows player variables
local function onPlayerJoin(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local gold = Instance.new("IntValue")
gold.Name = "Archives"
gold.Value = 0
gold.Parent = leaderstats
local items= Instance.new("IntValue")
items.Name = "Movies"
items.Value = 0
items.Parent = leaderstats
-- Create the Spaces stat
local spaces = Instance.new("IntValue")
spaces.Name = "Spaces"
spaces.Value = 5
spaces.Parent = leaderstats
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
I’ve tried to solve this by looking on the developer hub and watching various videos. If anyone can help me figure out what I did wrong let me know!