How do I fix my Data store script with multiple values?

Hello there.

I am not the best with Data stores as I didn’t use them often until now. I am trying to save multiple values in a folder at once.

local DataService = game:GetService("DataStoreService")
local DataStore = DataService:GetDataStore("PlayerInfo")

game.Players.PlayerAdded:Connect(function(Player)
	local stats = Instance.new("Folder")
	stats.Parent = game.ReplicatedStorage.PlayerInfo
	stats.Name = Player.Name.."Data"

	local GameStarted = Instance.new("BoolValue")
	GameStarted.Name = "HasPlayed"
	GameStarted.Parent = stats
	local CharacterName = Instance.new("StringValue")
	CharacterName.Name = "CharacterName"
	CharacterName.Parent = stats
	
	local playerUserId = "Player_"..Player.UserId

	local Data
	local success, errormessage = pcall(function()
		Data = DataStore:GetAsync(playerUserId)
	end)

	if success then
		stats:GetChildren().Value = Data
	end
	
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local playerUserId = "Player_"..Player.UserId

	local data = game.ReplicatedStorage.PlayerInfo:FindFirstChild(Player.Name.."Data"):GetChildren().Value
	
	local success, errormessage = pcall(function()
		DataStore:SetAsync(playerUserId, data)
	end)
	
	if success then
		print("Successfully saved.")
	else
		print("Error occured.")
		warn(errormessage)
	end
end)

I keep getting an error message.

2 Likes

Which line is the error message coming from in your script?

1 Like

It’s a error message from the pcall.

  Error occured.
  08:43:27.447 - Argument 2 missing or nil
1 Like

if you want to save multiple values inside a datastore I suggest using dictionaries
Example

local data = {
    ["Money"] = 10,
    ["HasPlayed"] = true
}

DataStore:SetAsync(data) -- setting the data
local sucess,data = pcall(function()
    return DataStore:GetAsync(key)
end) -- gathering the data
2 Likes

Thank you so much. I can finally finish this

1 Like