Leon_Kennedy0111 Posted March 7, 2023 Share Posted March 7, 2023 Hello, I wrote a JailTimer script and I faced this problem and I was confused. Can anyone help me? Console Errors: [07:17:17] WARNING: jail/server.lua:8: Bad argument @ 'setTimer' [Expected function at argument 1, got nil] [07:17:17] WARNING: jail/server.lua:18: Bad argument @ 'addEventHandler' [Expected element at argument 2, got nil] Problematic Script: --------------- Script By RxipeR --------------- --Arthur RxipeR --JailTimeR Script local timer = setTimer( timerJail, 1000,1 ) function timerJail() if getElementData(source,"jailTimer") > 0 then timeRoTamoomKon() triggerClientEvent ( source, "onSyncTime") resetTimer(timer) end end addEventHandler("onPlayerSpawn",source, function() if getElementData(source,"jailTimer") > 0 then if getElementData(source, "jailLoc") == 1 then spawnPlayer(source, 264.228515625, 77.505859375, 1001.0390625) setElementInterior ( source, 6 ) end end end ) function timeRoTamoomKon() local timeJailChande = getElementData(source,"jailTimer") local kamkon = tonumber(timeJailChande) - 1 setElementData(source, "jailTimer", tonumber(kamkon)) end Link to comment
Addlibs Posted March 7, 2023 Share Posted March 7, 2023 (edited) First issue: Lua is parsed and executed from top to bottom; by the time of the setTimer call, timerJail has not been declared or defined. You need to move the setTimer call after the timerJail function definition. Second issue: addEventHandler requires an element to bind onto (2nd argument) -- this means which element (and its children if propagation is enabled, which it is by default) should the event fire for -- and in your code it is an nil-value variable source, hence the error. source is defined within an event handler function, so getElementData(source, "jailLoc") is fine, source is the player that spawned. source outside that function, such as in the arguments passed to addEventHandler, it is undefined/nil. Change this to something like root (this is a pre-defined synonym for the value returned by getRootElement()). Edited March 7, 2023 by Addlibs 1 Link to comment
Leon_Kennedy0111 Posted March 7, 2023 Author Share Posted March 7, 2023 (edited) 19 minutes ago, Addlibs said: First issue: Lua is parsed and executed from top to bottom; by the time of the setTimer call, timerJail has not been declared or defined. You need to move the setTimer call after the timerJail function definition. Second issue: addEventHandler requires an element to bind onto (2nd argument), in your code it refers to an undefined variable "source", a nil value, hence the error. source is defined within an event handler function, so getElementData(source, "jailLoc") is fine, source is the player that spawned. source outside that function, such as in the arguments passed to addEventHandler, it is undefined/nil. Change this to something like root (this is a pre-defined synonym for the value returned by getRootElement()). Thanks Bro ! It Works | Fixed But it gave this new error |: [08:45:01] ERROR: jail/server.lua:20: attempt to compare number with boolean What Happen? Line 20: if getElementData(source,"jailTimer") > 0 then Edited March 7, 2023 by RxipeR Link to comment
Leon_Kennedy0111 Posted March 7, 2023 Author Share Posted March 7, 2023 (edited) What are these errors? How do I fix every error that the console gives? I really don't understand. I'm just getting familiar with the console errors. I'll solve some by myself, but I'm stuck. Help me. attempt to compare number with boolean attempt to compare number with nil [Expected element at argument 1, got boolean] [Expected element at argument 1, got nil] I have a lot of problems with these errors and my console is full of these errors! Someone help me understand What does this mean? Please explain nil and boolean And how do I fix all the bad argument errors? more errors from console: [09:46:00] WARNING: jail-jadid/jail.lua:264: Bad argument @ 'destroyElement' [Expected element at argument 1, got boolean] [09:46:00] WARNING: jail-jadid/jail.lua:265: Bad argument @ 'destroyElement' [Expected element at argument 1, got boolean] [09:46:00] ERROR: jail-jadid/jail.lua:268: Bad argument @ 'setElementData' [Expected element at argument 1, got nil] Edited March 7, 2023 by RxipeR Link to comment
Addlibs Posted March 7, 2023 Share Posted March 7, 2023 These errors mean you're attempting to compare incomparable values, for example, a bool with a number. Is true > 1, < 1? It doesn't make sense. getElementData returns a 'false' value when there is no data stored under the given key -- all you have to do is prevent the comparison from happening if the returned value is not a number: local returnedValue = getElementData(someElement, "someKey") -- options: if (returnedValue) then if (returnedValue > 5) then -- valid end end if (returnedValue and returnedValue > 5) then -- valid end -- the following two will also work if the returned value is a string that contains arabic numbers in base-10, e.g. ("1000" (string) => 1000 (number)) if (tonumber(returnedValue) and tonumber(returnedValue) > 5) then -- valid end local returnedValue = tonumber(getElementData(someElement, "someKey")) if (returnedValue and returnedValue > 5) then -- valid end Link to comment
Leon_Kennedy0111 Posted March 7, 2023 Author Share Posted March 7, 2023 1 hour ago, Addlibs said: These errors mean you're attempting to compare incomparable values, for example, a bool with a number. Is true > 1, < 1? It doesn't make sense. getElementData returns a 'false' value when there is no data stored under the given key -- all you have to do is prevent the comparison from happening if the returned value is not a number: local returnedValue = getElementData(someElement, "someKey") -- options: if (returnedValue) then if (returnedValue > 5) then -- valid end end if (returnedValue and returnedValue > 5) then -- valid end -- the following two will also work if the returned value is a string that contains arabic numbers in base-10, e.g. ("1000" (string) => 1000 (number)) if (tonumber(returnedValue) and tonumber(returnedValue) > 5) then -- valid end local returnedValue = tonumber(getElementData(someElement, "someKey")) if (returnedValue and returnedValue > 5) then -- valid end Bravo Now I understand what happened Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now