Argument 2 missing or nil - Server - DataStore:40 error on datastore script

  1. What do you want to achieve?

I want to not get this error since I have no idea how to fix it.

  1. What is the issue?

The issue is that I’m getting an error called “Argument 2 missing or nil” and have no idea how to fix it nor why it happened.

script:

local DataStore = game:GetService("DataStoreService")
local GameDataStore = DataStore:GetDataStore("GameDataStore")


game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = leaderstats
	
	local data
	
	local success, errormessage = pcall(function()
		data = GameDataStore:GetAsync(player.UserId.."-cash")
	end)
	
	if success then
		cash.Value = data
	else
		warn(errormessage)
		print("There was a error while getting your data")
	end
end)



game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		GameDataStore:SetAsync(player.UserId.."-cash"..player.leaderstats.Cash.Value)
	end)
	
	if success then
		print("Player Data Saved")
	else
		print("There was a error while saving data")
		warn(errormessage)
	end
end)
  1. What solutions have you tried so far?

I tried to look on solutions of the developer hub however I just got confused.

Here you are concating your key with the value you’re going to save. Instead, use a , to separate arguments.

GameDataStore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value)

alright I’ll try that right now.