What's wrong with my code?
#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


Messages In This Thread
What's wrong with my code? - by tonibm19 - 04-28-2013, 09:04 PM
RE: What's wrong with my code? - by NiLSPACE - 04-28-2013, 09:46 PM
RE: What's wrong with my code? - by xoft - 04-28-2013, 11:40 PM
RE: What's wrong with my code? - by tonibm19 - 04-29-2013, 04:14 AM
RE: What's wrong with my code? - by NiLSPACE - 04-29-2013, 04:20 AM
RE: What's wrong with my code? - by tonibm19 - 04-29-2013, 04:24 AM
RE: What's wrong with my code? - by xoft - 04-29-2013, 04:38 AM
RE: What's wrong with my code? - by tonibm19 - 04-29-2013, 04:41 AM



Users browsing this thread: 1 Guest(s)