DarkRP / AddChatComman()

So, I never actually used this function of DarkRP before. It seems amazingly simple yet it seems to defy me. Maybe I am also amazingly simple :frowning:
My test code:



local function SSAFire(ply,cmd,args)
	local tar = FindPlayer(args[1])
	if team.GetName(ply:Team()) == "Mayor" then
		if team.GetName(tar:Team()) == "Secret Service Agent" then
			tar:TeamBan()
			tar:ChangeTeam(TEAM_CITIZEN, true)
			NotifyAll(1, 4, string.format(LANGUAGE.demoted, tar:Nick() .. " By The Mayor"))
		end
	end
	return ""
end
AddChatCommand("/fire", SSAFire)


However, when typing in chat the command simple removes the command from your chat string and submits the text. So:

/fire pantho
would say in chat
pantho

Can’t see why, anyone got any ideas?

Try this instead:

[lua]
local function SSAFire(ply,cmd,args)
local tar = FindPlayer(args[1])
if ply:Team() == TEAM_MAYOR then
if tar:Team() == TEAM_SSA then
tar:TeamBan()
tar:ChangeTeam(TEAM_CITIZEN, true)
NotifyAll(1, 4, string.format(LANGUAGE.demoted, tar:Nick() … " By The Mayor"))
end
end
return “”
end
AddChatCommand("/fire", SSAFire)
[/lua]

Change the TEAM_SSA to whatever the global is for Secret Service Agent.

Tried both ways, the code works fine as an ordinary function or concommand.

AddChatCommand passes args as a string, not as a table. Change FindPlayer(args[1]) to FindPlayer(args) and then try. There is also no cmd argument passed by AddChatCommand.

You should also have a ValidEntity check in there to check the player they’ve entered is actually a player.

Try this:
[lua]
local function SSAFire(ply, args)
local tar = FindPlayer(args)
if not ValidEntity( tar ) then
Notify( ply, 1, 4, “Could not find player: " … args )
return “”
end
if ply:Team() == TEAM_MAYOR then
if tar:Team() == TEAM_SSA then
tar:TeamBan()
tar:ChangeTeam(TEAM_CITIZEN, true)
NotifyAll(1, 4, string.format(LANGUAGE.demoted, tar:Nick() … " By The Mayor”))
end
end
return “”
end
AddChatCommand("/fire", SSAFire)[/lua]

The string explains all, thanks Drake.

I do use ply:IsValid(), just I removed a lot of the code for testing.