Jump to content

Bad Argument @ 'getPlayerName'


Seif

Recommended Posts

This is killing me. This specific line is giving me the Bad Argument error yet I see nothing wrong

local pname = getPlayerName(playerSource)

It's in a command. playerSource is the first parameter in addCommandHandler so it's the player.

For some reason, pname is a boolean. This debug line tells me that I can't concatenate pname because it's a boolean value.

outputChatBox("Name: "..pname..".", playerSource)

I can't find the problem at all.

EDIT: OK this is awkward. I think it's due to me disabling /me that it's doing this, because I pasted the same lines in another command and it worked. In that case, here's how I disabled the original /me:

addEventHandler("onPlayerChat", getRootElement(), 
function (message, messageType)
outputChatBox(message)
if messageType == 1 then
cancelEvent()
triggerEvent("commandMe", source, "/me", message)
end
end
)
 
--then I added commandMe as an event so I can trigger it
addEvent("commandMe", true)
addEventHandler("commandMe", getRootElement(), commandMe)

Edited by Guest
Link to comment
Is it clientside?

Then the first parameter of a command handler function is the command text.

Maybe post the whole code.

Also try a type(playerSource) and a tostring(playerSource) to see, what it really is and post results.

I just edited my post. And no, it's server side.

Link to comment
function commandMe(playerSource, command, param)
local arg = tostring(param)
local pname = getPlayerName(playerSource)
outputChatBox("Name: "..pname..".", playerSource)
if not param then
outputChatBox("#B4B5B7USAGE: /me [action]", playerSource, 0, 0, 0, true)
return false
end
local cmd = string.sub(arg, 1)
outputRadiusChat(playerSource, "* "..pname.." "..cmd, 20.0, 188, 94, 255, false)
end
addCommandHandler("me", commandMe)

I'm also struggling with string.sub. Isn't it suppose to help me extend param so it doesn't only catch 1 word, but all?

Link to comment

the second argument of triggerEvent is the element to trigger on (the element that will be source in any functions attached to it)

the first argument in your commandMe function will be '/me', not the player

this strikes me as quite an odd way to do it though, why not call the function directly rather than using an event? or simply even add your commandMe code into your onPlayerChat handle. there is no need to add your own /me command handler

Link to comment

Correct, but that really seems to be a popular mistake.

The source, which you provide, is never an argument, although it is in some way a "hidden" argument, if you will.

But as R3mp said, you should directly call the function rather than using triggerEvent.

Regarding your second problem, I don't really get, what you're trying to do.

But anyway, you're using string.sub incorrectly.

The second argument is not a word index, but rather a character index from where to start.

If arg would be "blah", your string.sub would return "lah".

http://www.lua.org/manual/5.1/manual.ht ... string.sub

Edited by Guest
Link to comment

string.sub splits a string at a given position ( http://lua-users.org/wiki/StringLibraryTutorial ),

to gather all input from a command you will need to use '...' ( check server example 4 here )

but again, you dont need to create your own function to handle this:

addEventHandler("onPlayerChat", getRootElement(), 
function (message, messageType)
outputChatBox(message)
if messageType == 1 then
cancelEvent()
        outputRadiusChat(source, "* "..getPlayerName(source).." "..message, 20.0, 188, 94, 255, false)
end
end
)

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