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

Attempt to preform arithmetic on a nil value?

Asked by 5 years ago

Hi - Right now this is my code but it doesn't seem to be working...

if tlc ~= nil then
    tlc.Name = tostring(tonumber(tlc.Name) + 1)
end

it's in a for loop and i'm trying to make its name go up by one for example

First time entire for loop runs, name = 1 Second time, name = 2 etc... but i get this error:

Attempt to preform arithmetic on a nil value

which i sort of understand, but how can a part's Name be nil?

0
1 + nil = error. For your information, if tonumber cannot transform the string into a number, it returns nil. User#26971 0 — 5y
0
tonumber(tlc.Name) is nil, which means there is a non-digit or decimal value such as a , or a * theking48989987 2147 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

Sorry, my bad...

I had a part that was called something that wasn't a number and that messed everything up... So here's my solution if anyone else was having this problem:

if tonumber(part.Name) ~= nil then
    -- do math on name
end
Ad
Log in to vote
-1
Answered by 5 years ago
Edited 5 years ago

From the sound of things, it would appear that, "tlc" is under a index of sorts, I would suggest using :FindFirstChild()

FindFirstChild takes into account if something is nil or not, while just doing for example

workspace.Part

Up here is just a destination. ^

workspace:FindFirstChild("Part") 

This is the method for checking, the difference is it finds parts, but also has the ability to see stuff, such as if it is even found while if you just put the destination without that built in function in, it will error out all the time. So when checking if a part is found, or if anything is in anywhere in your game explorer, make sure to use :FindFirstChild()

0
I dont think that is the problem theking48989987 2147 — 5y
0
Yeah, that's not the problem. DeceptiveCaster 3761 — 5y

Answer this question