Jump to content

Why is it wrong?


Norby127

Recommended Posts

Hi guys. Because of something unknown error my script can't find the target player and I don't rly know why. I didn't get any error code. Could you help me please?

The source code:

function locatePlayer( source, command, target, ... )

    local args={...}
    local countArgs = #args
   
    if (countArgs < 1) or countArgs > 1 then
        outputChatBox("Usage: /pos <name>", source)
    else

        local targetPlayer = getPlayerFromName(target)              
        if (targetPlayer) then
            outputChatBox(target)
        else
            outputChatBox("Not found.")
        end    
    end
end
addCommandHandler ("pos", locatePlayer)
Link to comment

What is countArgs being used for? I don't think you need any of the variadic parameters. Your current code takes target as the first argument, and packs the rest of the arguments into args, and then you tested it for count of 1, meaning you had to provide two arguments, the target name and any string afterwards which is just discarded.

function locatePlayer( source, command, target )
  if (not target) then -- if target wasn't specified
    outputChatBox("Usage: /pos <name>", source)
    return -- return (nil) early, makes code cleaner -- less nesting. Control doesn't proceed beyond here if the if-case was true
  end
  
  local targetPlayer = getPlayerFromName(target)              
  if (targetPlayer) then
    outputChatBox(target)
  else
    outputChatBox("Target '" .. target .. "' not found.")
  end    
end
addCommandHandler ("pos", locatePlayer)

 

Link to comment
  • Moderators

With both codes, if you have "John Doe" and "John Wick" on your server and you try to locate "John Wick" then it will only look for "John" and will return the 1st one it finds in the player list. If you are lucky you will get "John Wick" but there are chances it will return "John Doe" too.

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