Argument #2 is missing or nil in database

So I’m currently scripting on my friend’s game, and I made a database script for the game here’s the code :

local DS = game:GetService("DataStoreService")
local PlayerData = DS:GetDataStore("DifficultyChartData")
local Players = game:GetService("Players")

_G.PlayerData = {}

-- Inserts Sample Data
local function SampleData(Player)
	_G.PlayerData[Player.UserId] = {
		["Team"] = "Effortless",
		["Checkpoint"] = 0,
		["Deaths"] = 0
	}
end

--Loads the Data
local function LoadData(Player)
	local Data = nil
	Data = PlayerData:GetAsync(Player.UserId)
	
	if not Data then
		SampleData(Player)
	end
	
	_G.PlayerData[Player.UserId] = Data
end

-- Saves the data this is the part where it errors.
local function SaveData(Player)
	 PlayerData:SetAsync(Player.UserId, _G.PlayerData[Player.UserId]) --this is the line where it errors
	
	_G.PlayerData[Player.UserId] = nil
end

Players.PlayerAdded:Connect(function(Player)
    LoadData(Player)
end)

Players.PlayerRemoving:Connect(function(Player)
	SaveData(Player)
end)

it seems like the Global Variable _G.PlayerData[Player.UserId] is nil because it gives an error when it saves the data called Argument #2 is missing or nil in this case Argument #2 isn’t missing at all so it’s nil then.
API Service and HTTP Service is enabled.

I would appreciate it if you can help me with this.

1 Like

I do belive your problem lies in

local function LoadData(Player)
	local Data = nil
	Data = PlayerData:GetAsync(Player.UserId)
	
	if not Data then
		SampleData(Player)
	end
	
	_G.PlayerData[Player.UserId] = Data
end

specifically given that new players wont actually have any data and so _G.PlayerData[Player.UserId] will equal nil
instead
you could fix the code with i gues something like this

_G.PlayerData = {}

-- Inserts Sample Data
local function SampleData(Player)
	local data = {
		["Team"] = "Effortless",
		["Checkpoint"] = 0,
		["Deaths"] = 0
	}
	return data
end

--Loads the Data
local function LoadData(Player)
	local Data = nil
	Data = PlayerData:GetAsync(Player.UserId)

	if not Data then
	Data =	SampleData(Player)
	end

	_G.PlayerData[Player.UserId] = Data
	print(_G.PlayerData[Player.UserId])
end

-- Saves the data this is the part where it errors.
local function SaveData(Player)
	PlayerData:SetAsync(Player.UserId, _G.PlayerData[Player.UserId]) --this is the line where it errors

	_G.PlayerData[Player.UserId] = nil
end

Players.PlayerAdded:Connect(function(Player)
	LoadData(Player)
end)

Players.PlayerRemoving:Connect(function(Player)
	SaveData(Player)
end)

all ive really done is made it return a value for Data
so that the variable Data has value so that the line

_G.PlayerData[Player.UserId] = Data

wont cause the G_.PlayerData[Player.UserId] to equal nil

Whenever you use the SetData() function, it is overridden by the line:

_G.PlayerData[Player.UserId] = Data

Since data is never defined, it sets it to nil.

To better explain it, the function is settings the global value to the, I assume, default data table. However, it is basically reverted immediately afterward. If no data has yet been created, it sets the value of the _G.PlayerData[Player.UserId] to nil.

To prevent this from happening, I would recommend using the SampleData function to return the default data and set the Data variable to what it returns, rather than setting it, and immediately undoing the action.

1 Like

exactly was I was trying to go for

Turns out I forgot to set the Data = SampleData(Player) thank you