Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

why does this error comes up?

Asked by
mine248 40
9 years ago

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)
0
Would it be oldValue.Value? User#9949 0 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

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.

Ad

Answer this question