What's wrong with my code?
#1
What's wrong with this code?
Code:
function Initialize(Plugin)
       PLUGIN = Plugin

    Plugin:SetName( "MoreCommands" )
    Plugin:SetVersion( 1 )

       PluginManager = cRoot:Get():GetPluginManager()

       PluginManager:BindCommand("/kill",    "morecommands.kill",    HandleKillCommand,    " - Kill some player");
       LOG("Initialized " .. PLUGIN:GetName() .. " v" .. PLUGIN:GetVersion())
    return true
end

function HandleKillCommand( Split, Player )
       local OtherPlayer = OtherPlayer:GetName()

       if Split[2] == nil then
           Player:SendMessage( cChatColor.Green .. "Usage: /kill [Player]" )
       end
       if OtherPlayer == Split[2] then
        OtherPlayer:TakeDamage( 100 , Player )    
           return true
    end
end
Reply
Thanks given by:
#2
i think you have to change local OtherPlayer = OtherPlayer:GetName() to
local OtherPlayer = Split[2] since OtherPlayer is a nil value but i'm not sure.

Maybe cRoot:Get():FindAndDoWithPlayer(Split[2], functionhere) works. like this:
local dowithplayer = function(OtherPlayer)
   OtherPlayer:TakeDamage(100, OtherPlayer)
end
if cRoot:Get():FindAndDoWithPlayer(Split[2], dowithplayer) == false then
    Player:SendMessage( "Player not found" )
else
    Player:SendMessage( "Player " .. Split[2] .. " is killed")
end
EDIT:
Not sure if it works though.
Reply
Thanks given by: tonibm19
#3
local OtherPlayer = OtherPlayer:GetName();
This doesn't make much sense. You're creating a new variable named OtherPlayer, initialized to nil, then you call GetName() on it (which fails) and finally want to assign the name to the variable. It's very similar to this (erroneous) piece of code, that I don't think you'd ever write:
local MyNumber = MyNumber + 1;

STR_Warrior's solution should work fine, unless there are two players with similar names, such as "JohnSmith" and "JohnJones"; then trying to kill "John" could kill either. FindAndDoWithPlayer() selects the player "from a partial or complete player name" (documentation in cRoot.h). To fix that, I'd compare the OtherPlayer's name with Spli[2] again in the callback function:
local HasKilled = false;
local KillPlayer = function(OtherPlayer)
   if (OtherPlayer:GetName() == Split[2]) then
      OtherPlayer:TakeDamage(100, OtherPlayer);
      HasKilled = true;
   end
end

cRoot:Get():FindAndDoWithPlayer(Split[2], KillPlayer);
if (HasKilled) then
    Player:SendMessage( "Player " .. Split[2] .. " is killed");
else
    Player:SendMessage( "Player not found" );
end
Reply
Thanks given by: tonibm19
#4
I tried all your codes and it don't works. When I try to kill some player it gives me this error:
LUA: Plugins/MoreCommands/main.lua:16: error in function 'TakeDamage'.
argument #2 is 'number'; 'cPawn' expected.

Maybe is an error in the TakeDamage function
Reply
Thanks given by:
#5
then you should propably just OtherPlayer:TakeDamage(OtherPlayer) or something.
Reply
Thanks given by:
#6
(04-29-2013, 04:20 AM)STR_Warrior Wrote: then you should propably just OtherPlayer:TakeDamage(OtherPlayer) or something.
Dont work

I also tried with cPawn_DoTakeDamage(100, OtherPlayer)
And gives me this error:
LUA: Plugins/MoreCommands/main.lua:16: attempt to call method 'cPawn__DoTakeDamage' (a nil value)
Reply
Thanks given by:
#7
There are three TakeDamage functions in cPawn, none of them matches what you're calling:
/// Makes this pawn take damage from an attack by a_Attacker. Damage values are calculated automatically and DoTakeDamage() called
void TakeDamage(cPawn & a_Attacker);
	
/// Makes this pawn take the specified damage. The final damage is calculated using current armor, then DoTakeDamage() called
void TakeDamage(eDamageType a_DamageType, cPawn * a_Attacker, int a_RawDamage, double a_KnockbackAmount);

/// Makes this pawn take the specified damage. The values are packed into a TDI, knockback calculated, then sent through DoTakeDamage()
void TakeDamage(eDamageType a_DamageType, cPawn * a_Attacker, int a_RawDamage, int a_FinalDamage, double a_KnockbackAmount);
The first one calculates damage based on what the attacker is wielding - not what you want here.
The second one subtracts armor value from RawDamage and deals that to the pawn - could be used, but the third one is better.
The third one uses everything that you specify and just deals the damage.

Note: Don't set a_Attacker to the player issuing the command, otherwise if we ever get the plugin that checks player distance before allowing damage, it would most likely forbid the kill. Use nil instead:
OtherPlayer:TakeDamage(dtInVoid, nil, 1000, 1000, 0);

In rev 1429 and later, you should also use dtAdmin or dtPlugin instead of dtInVoid.
Reply
Thanks given by: tonibm19
#8
(04-29-2013, 04:38 AM)xoft Wrote: There are three TakeDamage functions in cPawn, none of them matches what you're calling:
/// Makes this pawn take damage from an attack by a_Attacker. Damage values are calculated automatically and DoTakeDamage() called
void TakeDamage(cPawn & a_Attacker);
	
/// Makes this pawn take the specified damage. The final damage is calculated using current armor, then DoTakeDamage() called
void TakeDamage(eDamageType a_DamageType, cPawn * a_Attacker, int a_RawDamage, double a_KnockbackAmount);

/// Makes this pawn take the specified damage. The values are packed into a TDI, knockback calculated, then sent through DoTakeDamage()
void TakeDamage(eDamageType a_DamageType, cPawn * a_Attacker, int a_RawDamage, int a_FinalDamage, double a_KnockbackAmount);
The first one calculates damage based on what the attacker is wielding - not what you want here.
The second one subtracts armor value from RawDamage and deals that to the pawn - could be used, but the third one is better.
The third one uses everything that you specify and just deals the damage.

Note: Don't set a_Attacker to the player issuing the command, otherwise if we ever get the plugin that checks player distance before allowing damage, it would most likely forbid the kill. Use nil instead:
OtherPlayer:TakeDamage(dtInVoid, nil, 1000, 1000, 0);

In rev 1429 and later, you should also use dtAdmin or dtPlugin instead of dtInVoid.
Thx it worked
Reply
Thanks given by:




Users browsing this thread: 3 Guest(s)