So I had a DataStore script, and a error went up and said "ServerScriptService.Script:22: attempt to perform arithmetic on local 'newValue' (a nil value)". Are there any solutions?
Faulty code:
local replicated = game:GetService("ReplicatedStorage")
local DataStore = game:GetService("DataStoreService"):GetDataStore("essintials")
game.Players.PlayerAdded:connect(function(player)
--create leadstats
local leadstats = Instance.new("IntValue")
leadstats.Name = "leaderstats"
leadstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Value = 0
money.Parent = leadstats
local points = Instance.new("IntValue")
points.Name = "Points"
points.Parent = player
--datastore
local key = "user_" .. player.userId
--increase by 100 money per entering
DataStore:UpdateAsync(key, function(oldValue)
local newValue = oldValue --maybe nil
newValue = newValue + 100
money.Value = newValue
return newValue
end)
end)
game.Players.PlayerRemoving:connect(function(player)
local money = player.leaderstats.Money
--save finale
local key = "user_" .. player.userId
DataStore:UpdateAsync(key,function(oldValue)
local newValue = oldValue --maybe nil
newValue = money.Value
return newValue
end)
end)

1 comment
Would it be oldValue.Value?