Jump to content

ERROR: attempt to compare number with nil


Recommended Posts

I have tried to fix this problem but have not succeeded. Please hope someone can help me with this erorr.

ERROR: attempt to compare number with nil

 

function Seleccion()	 	
		 
		 fadeCamera(false,0.0)
		 showChat(false)
		 for i = 1, 8 do
		 guiSetVisible(buttons[i],true)	 
		 end
		 
		 if hour >= 6 and hour <= 11 then	-- ERROR HERE 
		 guiSetVisible(Temprano,true)
		 
		 elseif hour >= 12 and hour <= 17 then
		 guiSetVisible(Tarde,true)		 
		 
		 elseif hour >= 18 and hour <= 23 then
		 guiSetVisible(Noche,true)
	
		 elseif hour >= 1 and hour <= 5 then
		 guiSetVisible(Madrugada,true)
		 end
		 showCursor(true)		 
end
addEvent("Seleccion",true)
addEventHandler("Seleccion",getRootElement(),Seleccion)

 

Link to post
  • Administrators

@Maruchan your thread has been moved into Scripting section. Scripting Tutorials is for tutorials only. This is the third time I move your threads, please remember this next time you make a thread. :)

  • Like 1
Link to post

Hello,

It looks like the variable "hour" is not declared or has no value set (at least in the code you shared).
This is why it throws you an error that it can't compare "nothing" with a number.

You will have to use getTime (game current time) or getRealTime (server/client current time).

Link to post
function Seleccion()	 
		 
		 fadeCamera(false,0.0)
		 showChat(false)
		 for i = 1, 8 do
		 guiSetVisible(buttons[i],true)	 
		 end
		 
		 local time = getRealTime()
		 local hours = time.hour
		 local minutes = time.minute
		 local seconds = time.second
		 
		 if hour >= 6 and hour <= 11 then	 
		 guiSetVisible(Temprano,true)
		 
		 elseif hour >= 12 and hour <= 17 then
		 guiSetVisible(Tarde,true)		 
		 
		 elseif hour >= 18 and hour <= 23 then
		 guiSetVisible(Noche,true)
	
		 elseif hour >= 1 and hour <= 5 then
		 guiSetVisible(Madrugada,true)
		 end
		 showCursor(true)		 
end
addEvent("Seleccion",true)
addEventHandler("Seleccion",getRootElement(),Seleccion)

this?

Link to post

Kind of.

You won't need the minutes and seconds variables.
And you will have to rename the "hours" var to "hour".
As you can see in the if operators it compares the "hour" var with numbers.

Link to post
Posted (edited)

Now???

 

function Seleccion()	 
		 
		 fadeCamera(false,0.0)
		 showChat(false)
		 for i = 1, 8 do
		 guiSetVisible(buttons[i],true)	 
		 end
		 
		 local time = getRealTime()
		 local hour = time.hour

		 if hour >= 6 and hour <= 11 then	 
		 guiSetVisible(Temprano,true)
		 
		 elseif hour >= 12 and hour <= 17 then
		 guiSetVisible(Tarde,true)		 
		 
		 elseif hour >= 18 and hour <= 23 then
		 guiSetVisible(Noche,true)
	
		 elseif hour >= 1 and hour <= 5 then
		 guiSetVisible(Madrugada,true)
		 end
		 showCursor(true)		 
end
addEvent("Seleccion",true)
addEventHandler("Seleccion",getRootElement(),Seleccion)

 

Edited by Maruchan
Link to post
6 hours ago, Maruchan said:

Now???

 


function Seleccion()	 
		 
		 fadeCamera(false,0.0)
		 showChat(false)
		 for i = 1, 8 do
		 guiSetVisible(buttons[i],true)	 
		 end
		 
		 local time = getRealTime()
		 local hour = time.hour

		 if hour >= 6 and hour <= 11 then	 
		 guiSetVisible(Temprano,true)
		 
		 elseif hour >= 12 and hour <= 17 then
		 guiSetVisible(Tarde,true)		 
		 
		 elseif hour >= 18 and hour <= 23 then
		 guiSetVisible(Noche,true)
	
		 elseif hour >= 1 and hour <= 5 then
		 guiSetVisible(Madrugada,true)
		 end
		 showCursor(true)		 
end
addEvent("Seleccion",true)
addEventHandler("Seleccion",getRootElement(),Seleccion)

 

Do u want a real time or IN-Game time? 

Link to post

Well that should work, please note that if used on client side that returns the clients computer time witch might be different from server time! A work around will be to set the game time as the real time on server side and get the game time on the client. If not you can set it as an elementData on root element and update that data every hour on server 

  • Like 1
Link to post
What I would like is for the weather to be logical according to the schedule.
But since I am a newbie, I am not sure which IDs to use to achieve this.
Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...