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

DataStore, attempt to perform arithmetic on local 'oldValue' (a nil value)?

Asked by
Scout9 17
8 years ago
-- remotefunctions kk

--serverscript


DS = game:GetService("DataStoreService"):GetDataStore("votes")
event = game.ReplicatedStorage.Events.VotePlayer1

event.OnServerEvent:connect(function()
DS:UpdateAsync("player1", function(oldValue) return oldValue + 1 end)

end)



-- inside the gui button
-- parenting
rs = game.ReplicatedStorage
events = rs.Events
votefunction = events:FindFirstChild("VotePlayer1")
-- script

local button = script.Parent
button.MouseButton1Down:connect(function()
votefunction:FireServer()
end)

This gives this error:

ServerScriptService.Script:13: attempt to perform arithmetic on local 'oldValue' (a nil value)

How would I define a value, this are all scripts I have..

but I have no clue how to fix this

1 answer

Log in to vote
2
Answered by 8 years ago

The solution is in the error message. Apparently "oldValue" has returned nil for some reason, or wasn't returned at all. We can fix this by simply adding an "and" and "or" statement like so:

DS = game:GetService("DataStoreService"):GetDataStore("votes")
event = game.ReplicatedStorage.Events.VotePlayer1

event.OnServerEvent:connect(function()
    DS:UpdateAsync("player1", function(oldValue) 
        return oldValue and oldValue+1 or 1 -- if old, then add 1. if not, return 1
    end)
end)

rs = game.ReplicatedStorage
events = rs.Events
votefunction = events:FindFirstChild("VotePlayer1")
local button = script.Parent

button.MouseButton1Down:connect(function()
    votefunction:FireServer()
end)
0
Thanks, you fixed it! Scout9 17 — 8y
0
No problem. CodingEvolution 490 — 8y
Ad

Answer this question