Posts: 1,162
Threads: 68
Joined: Mar 2013
Thanks: 245
Given 125 thank(s) in 100 post(s)
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
Posts: 4,628
Threads: 115
Joined: Dec 2011
Thanks: 693
Given 494 thank(s) in 423 post(s)
04-28-2013, 09:46 PM
(This post was last modified: 04-28-2013, 10:27 PM by NiLSPACE.)
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.
Posts: 6,485
Threads: 176
Joined: Jan 2012
Thanks: 131
Given 1075 thank(s) in 852 post(s)
04-28-2013, 11:40 PM
(This post was last modified: 04-28-2013, 11:41 PM by xoft.)
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
Posts: 1,162
Threads: 68
Joined: Mar 2013
Thanks: 245
Given 125 thank(s) in 100 post(s)
04-29-2013, 04:14 AM
(This post was last modified: 04-29-2013, 04:15 AM by tonibm19.)
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
Posts: 4,628
Threads: 115
Joined: Dec 2011
Thanks: 693
Given 494 thank(s) in 423 post(s)
then you should propably just OtherPlayer:TakeDamage(OtherPlayer) or something.
Posts: 1,162
Threads: 68
Joined: Mar 2013
Thanks: 245
Given 125 thank(s) in 100 post(s)
04-29-2013, 04:24 AM
(This post was last modified: 04-29-2013, 04:35 AM by tonibm19.)
(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)
Posts: 6,485
Threads: 176
Joined: Jan 2012
Thanks: 131
Given 1075 thank(s) in 852 post(s)
04-29-2013, 04:38 AM
(This post was last modified: 04-29-2013, 04:40 AM by xoft.)
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.
Posts: 1,162
Threads: 68
Joined: Mar 2013
Thanks: 245
Given 125 thank(s) in 100 post(s)
(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
|