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

"Attempt to perform arithmetic on number and nil"?

I have the error 'attempt to perform arithmetic (sub) on number and nil. 9th line

local remote = game:GetService("ReplicatedStorage"):WaitForChild('GetCacheData')
local storage = game:GetService("ServerStorage")
local Cache = require(storage:WaitForChild('CacheData'))
local timeout = 5
local logged = {}

remote.OnServerInvoke = function(player, kind, ...)
	if not player or not kind then return false end
	if tick()-logged[player.UserId] < timeout then return false end
	local thisCache = Cache[kind]
	
	if #{...} < 1 then return thisCache end
	for _, k in pairs({...}) do
		thisCache = thisCache[k] or thisCache
	end
	
	logged[player.UserId] = tick()
	return thisCache
end

1 comment

0

logged[player.UserId] shouldn't be nil, try printing to see if it is nil, check if it's nil basically

1 answer

if you are not assigning the table value as logged[player.UserId] = (the number) then it will register as nil and error as your code did, i suggest printing the logged table to see the values and fixing your code from here as you have not shown the way your script adds values to the "logged" table

(edit: i see how it adds to the logged table, but that will not work as it adds it AFTER it attempts to do the math)

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