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