Some Help on Lua

I’m very new to the whole Lua scene.

So first of all… I’ve looked this up but the explanations I found simply make no sense to me…

Take the “customCheck” from DarkRP’s Wiki for example:


customCheck = function(ply) return ply:GetNWString("usergroup") == "donator" or ply:IsAdmin() end,

What the fuck does that “return” shit do?! I can decipher the rest of the code just fine. However, that damned return shit makes no sense to me. Can someone please give me an explanation for a beginner?

Scripts are able to call functions that can process input, and, depending on that input, can return an output.

return is a way to tell whoever called the function what the result of the function is. Think of a very simple function, such as one that adds two numbers.


function add(x, y)
	x + y
end

Very simple code, yes? But wait…where is the result stored? Nowhere. If it isn’t stored anywhere, how is the caller of the function supposed to know the output? This is why you need to return the output or result.


function add(x, y)
	return x + y
end

Then, you can do this.


local result = add(6, 4)

And the result is equal to 10.

I see… However, how is it used in the custom check? I mean it doesn’t use the stored result??

The result doesn’t need to be stored anywhere if you return the result itself. Maybe the code being on one line makes it look weirder. Hopefully this clears it up.


-- Bad.
local function customCheck(ply)
	ply:GetNWString("usergroup") == "donator" or ply:IsAdmin()
end

-- Good.
local function customCheck(ply)
	return ply:GetNWString("usergroup") == "donator" or ply:IsAdmin()
end

-- Not good, but valid.
local function customCheck(ply)
	local result = ply:GetNWString("usergroup") == "donator" or ply:IsAdmin()

	return result
end

So basically it tells the server to check … but without the “return” it would check, just not return the results?

The first piece of code would actually cause an error, but yes, the third piece of code (without the return) would perform the check and never tell what the result was.

Thank you so much bro! :smiley: I understand finally lmaooo!

I have never looked into DarkRP, but the way I imagine they use this is by doing something like this:
[lua]-- When trying to switch job
if jobtable[targetJob].customCheck(ply) then
– Allow ply to become job
end[/lua]
You have to imagine the return value replacing the function call. In which case it would be true if the player Is a donator or is an admin. It’s only when you call this function that the return value gets calculated, meaning that if the player later becomes a donator, it will return true from then on.