Data Store 2 (nil number arithmetic add number)


local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local Workspace = game:GetService("Workspace")

local DataStore2 = require(ServerScriptService.DataStore2)

-- Combine every key you use. This will eventually be the default, but for now read the "Gotchas" section to understand why we need this.
DataStore2.Combine("DATA", "Clicks")

Players.PlayerAdded:Connect(function(player)
	local clicksStore = DataStore2("Clicks", player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"

	local clicks = Instance.new("NumberValue")
	clicks.Name = "Clicks"
	clicks.Value = clicksStore:Get(0) -- The "0" means that by default, they'll have 0 points
	clicks.Parent = leaderstats

	clicksStore:OnUpdate(function(newPoints)
		-- This function runs every time the value inside the data store changes.
		clicks.Value = newPoints
	end)

	leaderstats.Parent = player
end)


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

game.Players.PlayerAdded:Connect(function(player)
	wait(1)
	local clicksStore = DataStore2("Clicks", player)
	clicksStore:Increment(1) -- Give them 1 point
end)


attempt to perform arithmetic (add) on nil and number

clicksStore:Increment(1) – Give them 1 point

I don’t use DataStore2 but could it perhaps be because the user doesn’t currently have a number saved in the datastore?

You should try doing clicksStore:Increment(1,0) instead of clicksStore:Increment(1)

The second parameter which was 0, is the default value of the data store, so if it’s not there, and the person doesn’t have any data then it can’t add 1

1 Like

wait for some reason that actually worked by why though? Like why add a 0?