Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

why does this error comes up?

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

0

Would it be oldValue.Value?

1 answer

Okay. So the reason why you get an error at line 22 is because you aren't adding anything to your local newvalue. It's the same as oldvalue.

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 + 100
        local addedValue = oldValue = newValue
        money.Value = addedValue
        return addedValue
    end)
end)

I'm not entirely sure if this works as I have not tested it. But feel free to ask questions and report future errors in the script.

This discussion is preserved as it was. New answers, comments, and votes are closed.