✓ 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
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
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.
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 :/