[DataStore2] Throwing a nil error on defined value

Hi all, I want to have two different values that are increased by clicking on different parts (DepositDebris and DepositGas) but DS2 keeps throwing a nil error for the Gas → Speed value even though I copied and pasted directly from the working Debris → Mass value. If anyone could take a look at this that’d be fantastic. ^^

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", "mass", "slots", "speed", "capacity")

Players.PlayerAdded:Connect(function(player)
	local massStore = DataStore2("mass", player)

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

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

	massStore:OnUpdate(function(newMass)
		-- This function runs every time the value inside the data store changes.
		mass.Value = newMass
	end)

	leaderstats.Parent = player
end)

Players.PlayerAdded:Connect(function(player)
	local speedStore = DataStore2("speed", player)

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

	local speed = Instance.new("NumberValue")
	speed.Name = "🏃‍♂️ Speed"
	speed.Value = speedStore:Get(16) -- The "0" means that by default, they'll have 0 points
	speed.Parent = leaderstats

	speedStore:OnUpdate(function(newSpeed)
		-- This function runs every time the value inside the data store changes.
		speed.Value = newSpeed
	end)

	leaderstats.Parent = player
end)


Workspace.DepositDebris.ClickDetector.MouseClick:Connect(function(player)
	local massStore = DataStore2("mass", player)
	massStore:Increment(1) -- Give them 1 point
end)

Workspace.DepositGas.ClickDetector.MouseClick:Connect(function(player)
	local speedStore = DataStore2("speed", player)
	speedStore:Increment(1) -- Give them 1 point
end)

The error thrown is

  12:06:02.817  ServerScriptService.DataStore2:249: attempt to perform arithmetic (add) on nil and number  -  Server - DataStore2:249
  12:06:02.817  Stack Begin  -  Studio
  12:06:02.817  Script 'ServerScriptService.DataStore2', Line 249 - function Increment  -  Studio - DataStore2:249
  12:06:02.817  Script 'ServerScriptService.Script', Line 56  -  Studio - Script:56
  12:06:02.817  Stack End  -  Studio

Any help is appreciated. :blush:

Hello! Could you tell me what is returned when you print out the massStore? It should print out something.

U have to make sure that player arg when click is a player which is a instance bc u instantly call the datastore player and go increment you dont know if the datastore you call data is turning back a nil value

I added a print() to the code, since it didn’t print anything by default. Here’s the output:

  14:03:09.004   ▼  {
                    ["combinedInitialGot"] = true,
                    ["combinedName"] = "mass",
                    ["combinedStore"] =  ▼  {
                       ["Name"] = "DATA",
                       ["UserId"] = 33726575,
                       ["afterSave"] = {},
                       ["beforeInitialGet"] = {},
                       ["beforeSave"] = "function",
                       ["bindToClose"] = {},
                       ["callbacks"] = {},
                       ["getQueue"] = Event,
                       ["getting"] = false,
                       ["haveValue"] = true,
                       ["savingMethod"] =  ▶ {...},
                       ["value"] =  ▶ {...},
                       ["valueUpdated"] = true
                    },
                    ["onUpdateCallbacks"] =  ▼  {
                       [1] = "function"
                    }
                 }  -  Server - Script:52

Not really on-topic, but why are using 2 PlayerAdded events in one script? Your also creating two different leaderstats folders which can mess a few things up.

I’m not sure what this means sorry :frowning: can you post an example?

Ah like I said, I just copy and pasted the working block and changed the values. I don’t really know datastores that well (read: at all) and don’t know what’s unnecessary. I’ll change that ^^

Edit: It was this that was causing the issue! I merged the two blocks and both values increase properly. Thank you (even if you thought it wasn’t on-topic)!