Norby127 Posted March 4, 2022 Share Posted March 4, 2022 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
Addlibs Posted March 4, 2022 Share Posted March 4, 2022 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 Citizen Posted March 9, 2022 Moderators Share Posted March 9, 2022 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
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