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.