How to save multiple values in a Data Store

Hello developers!

This is a tutorial on how to save multiple values in a datastore.

Alright, let’s not waste any time and get straight to the point.

First define your DataStoreService variable.

local DataStoreService = game:GetService("DataStoreService")

Then, define the DataStore you want to save the values in.

local PlayerData = DataStoreService:GetDataStore("PlayerData")

After that, make sure to create your leaderstats.

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr
    
    local coins = Instance.new("IntValue")
    coins.Name = "Coins"
    coins.Value = 0
    coins.Parent = leaderstats

    local gems = Instance.new("IntValue")
    gems.Name = "Gems"
    gems.Value = 0
    gems.Parent = leaderstats
end)

Now, let’s make the saving system.

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr
    
    local coins = Instance.new("IntValue")
    coins.Name = "Coins"
    coins.Value = 0
    coins.Parent = leaderstats

    local gems = Instance.new("IntValue")
    gems.Name = "Gems"
    gems.Value = 0
    gems.Parent = leaderstats
    
    local data
    local success, errormessage = pcall(function() -- pcall is for when an error occurs, the script won't break and catches the error.
        data = PlayerData:GetAsync("Player_" .. plr.UserId)
    end)
    
    if success then
        coins.Value = data
    else
        warn(errormessage)
    end
end)

Let’s make the PlayerRemoving function now!

game.Players.PlayerRemoving:Connect(function(plr)
    local success, errormessage = pcall(function()
        PlayerData:SetAsync("Player_" .. plr.UserId, plr.leaderstats.Coins.Value)
    end)
	
    if success then
        print("Player Data successfully saved!")
    else
        warn(errormessage)
    end
end)

BUT. We want multiple values in this DataStore.
So, let’s change it up a bit.

game.Players.PlayerRemoving:Connect(function(plr)
    local success, errormessage = pcall(function()
        PlayerData:SetAsync("Player_" .. plr.UserId, {
            Coins = plr.leaderstats.Coins.Value,
            Gems = plr.leaderstats.Gems.Value
        })
    end)
	
    if success then
        print("Player Data successfully saved!")
    else
        warn(errormessage)
    end
end)

There we go. Much better!
But now, we need to change the PlayerAdded function.
Alright, let’s do it.

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr
    
    local coins = Instance.new("IntValue")
    coins.Name = "Coins"
    coins.Value = 0
    coins.Parent = leaderstats

    local gems = Instance.new("IntValue")
    gems.Name = "Gems"
    gems.Value = 0
    gems.Parent = leaderstats
    
    local data
    local success, errormessage = pcall(function()
        data = PlayerData:GetAsync("Player_" .. plr.UserId)
    end)
    
    if success then
        coins.Value = data.Coins
        gems.Value = data.Gems
    else
        warn(errormessage)
    end
end)

There! That’s all we needed for this tutorial!

Here’s the full script for anyone can’t follow this tutorial:

local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr

	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Value = 0
	coins.Parent = leaderstats

	local gems = Instance.new("IntValue")
	gems.Name = "Gems"
	gems.Value = 0
	gems.Parent = leaderstats

	local data
	local success, errormessage = pcall(function()
		data = PlayerData:GetAsync("Player_" .. plr.UserId)
	end)

	if success then
		coins.Value = data.Coins
		gems.Value = data.Gems
	else
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, errormessage = pcall(function()
		PlayerData:SetAsync("Player_" .. plr.UserId, {
			Coins = plr.leaderstats.Coins.Value,
			Gems = plr.leaderstats.Gems.Value
		})
	end)

	if success then
		print("Player Data successfully saved!")
	else
		warn(errormessage)
	end
end)

This is a modified version of @AlvinBlox’s DataStore Tutorial. You should definitely watch his video:

4 Likes