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

Attempt to preform arithmetic on a nil value?

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?

2 comments

0

1 + nil = error. For your information, if tonumber cannot transform the string into a number, it returns nil.

0

tonumber(tlc.Name) is nil, which means there is a non-digit or decimal value such as a , or a *

2 answers

✓ Accepted answer

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

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

2 comments

0

I dont think that is the problem

0

Yeah, that's not the problem.

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