Jump to content

3 small questions


Recommended Posts

Hi there, I have 3 questions.

1: What do I exactly put behind the function, what arguments do I use? For example, function HelloWorld()

What do I place inbetween the brackets? I've seen people leave it empty, add thePlayer there, sometimes player or source, I get what's the difference but what's the point of adding elements there?

2: What exactly is a boolean?

3: This question is related to a script.

function setAdminRanks(theAdmin, cmd, thePlayer, rank)
    local account = getAccountName(getPlayerAccount(thePlayer))

It gives 2 warnings (which basically :Os up the script)

Warning: Bad Argument @ getPlayerAccount [expected element at argument 1, got nil

 

Warning: Bad Argument @ getAccountName [expected account at argument 1, got boolean]

Thanks.

Edited by marty000123
Link to comment
1 hour ago, marty000123 said:

1: What do I exactly put behind the function, what arguments do I use? For example, function HelloWorld()

What do I place inbetween the brackets? I've seen people leave it empty, add thePlayer there, sometimes player or source, I get what's the difference but what's the point of adding elements there?

HelloWorld(ArgumentsHere)

The arguments are the data processed by the function. You mentioned functions with a parameter called "thePlayer". The name suggests* that the function accept a player-type variable, and therefore performs actions on that player/using that player data.

*Suggest: Arguments names are just something to recognise that parameter. It's not a type or whatever. If you called that "foo" or "someVariableNameChosenRandomly" and rename all the reference of that variable inside the function then no difference is made. Names are used to let humans understand easily the code.

This is not a MTA question, so searching this on google about LUA (or every other programming/scripting language actually) will give you the info you need.

1 hour ago, marty000123 said:

2: What exactly is a boolean?

Boolean is a variable type. It can store information in only 2 state: true and false. In LUA, false is represented by the false-values: false and nil. Every other value is considered true.

This question is also not MTA related. You can have further informations using google with a general language.

 

1 hour ago, marty000123 said:

3: This question is related to a script.


function setAdminRanks(theAdmin, cmd, thePlayer, rank)
    local account = getAccountName(getPlayerAccount(thePlayer))

It gives 2 warnings (which basically :Os up the script)


Warning: Bad Argument @ getPlayerAccount [expected element at argument 1, got nil

 


Warning: Bad Argument @ getAccountName [expected account at argument 1, got boolean]

Reading errors and warning produced from the scripts is the first thing to do to "fix" and debug. The first warn tells you that argument 1 used in getPlayerAccount is nil.

If you didn't pass nil, then you probably are calling this function without the rigth parameters (leaving them undefined -> nil)
Probably this is a command handler (given the parameter names). In this case the passed player will be a string, and not a player-type variable.

The warn 2, since the first function fails and returns false (boolean), is here to let you know that the same problem happened here: an account-type var was expected, instead false was passed.

 

function setAdminRanks(theAdmin, cmd, thePlayerName, rank)
	local thePlayer = getPlayerFromName( thePlayerName )
	if not thePlayer then --if getPlayerFromName returned false then no player with that name was found
		return --"kill" the function
	end
	local accountName = getAccountName(getPlayerAccount(thePlayer))
	--do whatever you need, remember that rank parameter could be nil (unspecified)
end

 

Edited by LoPollo
  • Like 1
Link to comment

Thanks alot for your help!

About question 3, your solution didn't work. Maybe it's because you don't know what's going on. Here's the first part of the script, I hope it helps.

function setAdminRanks(theAdmin, cmd, thePlayer, rank)
    local acc = getAccountName(getPlayerAccount(theAdmin))
    local account = getAccountName(getPlayerAccount(thePlayer))
    local dev = table.concat({rank}, "dev")
    local helper = table.concat({rank}, "helper")
    local moderator = table.concat({rank}, "mod")
    local admin = table.concat({rank}, "admin")
    if not isObjectInACLGroup("user."..acc, aclGetGroup("Admin")) then return end
    if isObjectInACLGroup("user."..acc, aclGetGroup("Administrator")) then
        if account then
            if dev then
                outputChatBox("AdmCmd: You have set "..getPlayerName(p).."'s staff rank to Developer!", theAdmin, 0, 255, 0)
                aclGroupAddObject (aclGetGroup("Mapper"), "user."..account)
                aclGroupAddObject (aclGetGroup("Staff"), "user."..account)
                outputChatBox("AdmCmd: "..getPlayerName(theAdmin).." has set your staff rank to Developer!", thePlayer, 0, 255, 0)

I was experimenting with command handlers indeed :)

Edited by marty000123
Link to comment

I changed thePlayer -> thePlayerName, and then added thePlayer as player-type variable.

There was a variable called "p", that i couldn't find in the code, so i replaced with the name of the player.

I also replaced the table concats... yet i don't know what you want to do with them so maybe it's better to re-use the old code.

function setAdminRanks(theAdmin, cmd, thePlayerName, rank)
    local acc = getAccountName(getPlayerAccount(theAdmin))
    local thePlayer = getPlayerFromName( thePlayerName ) --replace the name (a string) with the player element
    if not thePlayer then
        outputChatBox( "Syntax error: \""..thePlayerName.."\" was not found.")
        return
    end
    local account = getAccountName(getPlayerAccount(thePlayer))
    if not rank then
        outputChatBox( "Syntax error: rank was not specified.")
        return
    end
    --I'm replacing here something that could be the equivalent of what you were doing. Is it right? If not replace with the old code
    local dev = rank.."dev"--local dev = table.concat({rank}, "dev")
    local helper = rank.."helper"--local helper = table.concat({rank}, "helper")
    local moderator = rank.."mod"--local moderator = table.concat({rank}, "mod")
    local admin = rank.."admin"--local admin = table.concat({rank}, "admin")

    if not isObjectInACLGroup("user."..acc, aclGetGroup("Admin")) then return end
    if isObjectInACLGroup("user."..acc, aclGetGroup("Administrator")) then
        if account then
            if dev then
                --p was undefined here
                aclGroupAddObject (aclGetGroup("Mapper"), "user."..account)
                outputChatBox("AdmCmd: You have set "..thePlayerName.."'s staff rank to Developer!", theAdmin, 0, 255, 0)
                aclGroupAddObject (aclGetGroup("Staff"), "user."..account)
                outputChatBox("AdmCmd: "..getPlayerName(theAdmin).." has set your staff rank to Developer!", thePlayer, 0, 255, 0)

Note: I did not closed the function. Also i did not check if types are correct with acl functions, since i don't remember them... If there are error, wiki will help you fix them in nanoseconds, otherwise reply here. As always, code was not tested.

Link to comment

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