DataStores - Beginners to Advanced

It was pseudo-code, to implement it, you would probably change your setUp function to

local function setUp(player)
	local userId = player.UserId
	local key = "Player_" .. userId

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"

	local click = Instance.new("IntValue")
	click.Name = "Clicks"
	local diamond = Instance.new("IntValue")
	diamond.Name = "Diamonds"
	local rebirth = Instance.new("IntValue")
	rebirth.Name = "Rebirths"

	local data = dataStore:GetAsync(key)

	click.Value = (data and data[1]) or 0
	diamond.Value = (data and data[2]) or 100
	rebirth.Value = (data and data[3]) or 0

	click.Parent = leaderstats
	diamond.Parent = leaderstats
	rebirth.Parent = leaderstats
	leaderstats.Parent = player
end

and your save function to

local function save(player)
	local userId = player.UserId
	local key = "Player_" .. userId
	local leaderstats = player:FindFirstChild("leaderstats")

	if leaderstats then
		local clickValue = leaderstats.Clicks.Value
        local diamondValue = leaderstats.Diamonds.Value
        local rebirthValue = leaderstats.Rebirths.Value
        local toSave = {clickValue, diamondValue, rebirthValue}

        dataStore:SetAsync(key, toSave)
	end
end
1 Like