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

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

-- 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

✓ Accepted answer

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)

2 comments

0

Thanks, you fixed it!

0

No problem.

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