Datastore2 - attempt to perform arithmetic (add) on nil and number

Trying to add points to the players leaderstats when they step on a part.

Getting this error and here is my code.

19:34:33.579 ServerScriptService.DataStore2:192: attempt to perform arithmetic (add) on nil and number - Server - DataStore2:192 19:34:33.579 Stack Begin - Studio 19:34:33.579 Script 'ServerScriptService.DataStore2', Line 192 - function Increment - Studio - DataStore2:192 19:34:33.579 Script 'Workspace.xpPart.Script', Line 12 - Studio - Script:12 19:34:33.579 Stack End - Studio

local debounce = false
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local DataStore2 = require(ServerScriptService.DataStore2)

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if debounce == false then
			debounce = true
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			local XpStore = DataStore2("Xp", player)
			XpStore:Increment(50) -- Give them 1 point
			wait(1)
			debounce = false
		end
	end
end)


The error is telling you that on Line 129 (or Line 12) of your code you have provided a nil value.

nil means nothing.

So, what are you trying to add a number to?

Find that item and figure out why it is “nothing”.

It’s most likely that you haven’t initialized your datastore i.e. setting the default score on a new player.

Pretty sure this needs a second argument, which is the default value when getting their data. Try using XpStore:Increment(50, 0)!

This is my leaderboard script

--//variables
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local workspace = game:GetService("Workspace")
local DataStore2 = require(ServerScriptService.DataStore2)

--//keys
DataStore2.Combine("DATA", "XP", "Gold", "Level", "RequiredXP")

Players.PlayerAdded:Connect(function(player)
	local XpStore = DataStore2("XP", player)
	local GoldStore = DataStore2("Gold", player)
	local LevelStore = DataStore2("Level", player)
	local RequireXPStore = DataStore2("RequiredXP", player)
	
	local leadersats = Instance.new("Folder")
	leadersats.Name = "leaderstats"
	
	local hiddenstats = Instance.new("Folder")
	hiddenstats.Name = "hiddenstats"
	
	local Xp = Instance.new("NumberValue")
	Xp.Name = "Xp"
	Xp.Value = XpStore:Get(0)
	Xp.Parent = hiddenstats
	
	local Gold = Instance.new("NumberValue")
	Gold.Name = "Gold"
	Gold.Value = GoldStore:Get(0)
	Gold.Parent = leadersats
	
	local Level = Instance.new("NumberValue")
	Level.Name = "Level"
	Level.Value = LevelStore:Get(1)
	Level.Parent = leadersats
	
	local RequiredXP = Instance.new("NumberValue")
	RequiredXP.Name = "RequiredXP"
	RequiredXP.Value = Level.Value * 150
	RequiredXP.Parent = hiddenstats
	
	XpStore:OnUpdate(function(delta)
		-- This function runs every time the value inside the data store changes.
		Xp.Value = delta
	end)
	
	GoldStore:OnUpdate(function(delta)
		-- This function runs every time the value inside the data store changes.
		Gold.Value = delta
	end)
	
	LevelStore:OnUpdate(function(delta)
		-- This function runs every time the value inside the data store changes.
		Level.Value = delta
	end)
	
	leadersats.Parent = player
	hiddenstats.Parent = player
	
	Xp.Changed:Connect(function(changed)
		if Xp.Value >= RequiredXP.Value then
			Xp.Value = 0
			Level.Value += 1
			RequiredXP.Value = Level.Value*150
		end
	end)
end)

I changed my script to add the value to the folder instead of trying to go through datastore and it worked, but it does not save in the datastore (I have SaveInStudio active in ServerStorage)

local debounce = false
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local DataStore2 = require(ServerScriptService.DataStore2)

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if debounce == false then
			debounce = true
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			local XpStore = DataStore2("Xp", player)
			XpStore:Increment(50, 0) 
			player.hiddenstats.Xp.Value += 50
			wait(1)
			debounce = false
		end
	end
end)


image

Edit: when i leave it prints in the console that it saves the xp data, but it does not, and nor does it save the level but it doesnt print that it saved the level value