Jump to content

Require returns boolean value


Lpsd

Recommended Posts

  • MTA Team
Posted (edited)

I've got this simple require code, and in my console it's returning as a boolean, instead of returning with the function ready to use

server.lua:

function checkStars(thePlayer)
	local wantedLevel = getPlayerWantedLevel ( thePlayer ) -- get the wanted level of the player
	return wantedLevel
end

theFunctions = { "checkStars" } -- list of functions available for other files, returned when using "require"

return theFunctions

 

Another .lua file, in the same directory:

getMain = require("server")

function getStars(quitType)
local gotStar = getMain.checkStars(source)
end
addEventHandler ( "onPlayerQuit", getRootElement(), getStars )

 

And the error from console:

ERROR: xml2\serverLogin.lua:4: attempt to index global 'getMain' (a boolean value)

 

I know this means the file either isn't being loaded (false), or for some reason "theFunctions" isn't being returned. Any thoughts?

Edited by LopSided_
Posted

I'm not sure, I haven't done this at all in MTA yet but try to make the values in your theFunctions table non-string based. Why would they use a string to call a function?

  • MTA Team
Posted
34 minutes ago, tosfera said:

I'm not sure, I haven't done this at all in MTA yet but try to make the values in your theFunctions table non-string based. Why would they use a string to call a function?

Literal facepalm. I don't know why I'd put it like that.

I tried this however and still no luck:

theFunctions = {}

function theFunctions.checkStars(thePlayer)
	local wantedLevel = getPlayerWantedLevel ( thePlayer ) -- get the wanted level of the player
	return wantedLevel
end


return theFunctions

 

Posted

I'm currently working with some pretty complex things on work in angular but that did give me an idea. Try it like;

theFunctions = { };

theFunctions.checkStars = function ( thePlayer )
    return getPlayerWantedLevel ( thePlayer );
end

return theFunctions

 

  • MTA Team
Posted (edited)

Weird, nothing changed with that either.

Maybe it's the way I'm calling it, by doing "getMain.checkStars(source)"

 

Edit: Yep, it was.

server.lua:

theFunctions = {}

function theFunctions:checkStars(thePlayer)
	local wantedLevel = getPlayerWantedLevel ( thePlayer ) -- get the wanted level of the player
	return wantedLevel
end


return theFunctions

 

other .lua file in same directory:

require("server") -- no need to put in variable
local gotStar = theFunctions:checkStars(source) -- use table name with function in

 

Edited by LopSided_
  • MTA Team
Posted

Yeah that works perfectly now, I'd missed changing the "." to ":" by mistake ^_^

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...