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

"Attempted to compare nil with number" what does that mean?

The script below is meant to turn off a light depending on the TimeOfDay. But the system logs "Attempted to compare nil with number" anyone have any thoughts on this?

while true do
		
	while tonumber(game.Lighting.TimeOfDay) < 8 or tonumber(game.Lighting.TimeOfDay) > 20 do
		wait(1)		
		script.Parent.PointLight.Brightness = 15
		script.Parent.Material = "Neon"
	end
	
	while tonumber(game.Lighting.TimeOfDay) > 8 and tonumber(game.Lighting.TimeOfDay) < 20 do
		wait(1)		
		script.Parent.PointLight.Brightness = 0
		script.Parent.Material = "SmoothPlastic"
	end
end

Thanks, RadioactiveSherbet

1 answer

✓ Accepted answer

#What's going on? You're trying to get a number of time of day, which is a string. When you use tonumber you turn the whole string into a number, but if the string, like time of day has ":" in them, it returns nil, because it isn't a number, and you cannot compare nil with a number.


#How to fix it You need to find a way to cancel out the ":" from the text. It looks like you only need the first 2 characters because you're only finding the hour. This can be solved with string.sub() .


#How does it fix my code? Like I said, the problem is the string with non-numerical characters in it becoming numbers, which returns nil. If we cut out a section of the string to only say the numbers, it won't return nil. For example:

print( string.sub("Hi Mom!", 1, 4) )

Will print out:

Hi M


#Final Code

while true do --:sub() is a shorter way to write string.sub().

    while tonumber(game.Lighting.TimeOfDay:sub(1,2)) < 8 or tonumber(game.Lighting.TimeOfDay:sub(1,2)) > 20 do
        wait(1)     
        script.Parent.PointLight.Brightness = 15
        script.Parent.Material = "Neon"
    end

    while tonumber(game.Lighting.TimeOfDay:sub(1,2)) > 8 and tonumber(game.Lighting.TimeOfDay:sub(1,2)) < 20 do
        wait(1)     
        script.Parent.PointLight.Brightness = 0
        script.Parent.Material = "SmoothPlastic"
    end
end

#How can I stop making the same problem? Check and be aware that non numerical characters like the ones in the time of day, will return nil when turned into a number, which is normal. The error is when you try to see if the "nil" is greater than or less than a number, which will error. Try to watch out for those and check other string manipulation functions to work with later if you get the same problem.



Hope it helps!

2 comments

0

This is how all answers should look like: Short but informative, Says the answer and gives the explanation, stops the person from making the same mistake, links given when needed, and clean. Just don't tryhard it like me.

0

Oh my goodness @LordDragonZord I am glad that you cared and wrote all this for me. I mean I was down-voted pretty hard hence the -7 :/

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