Author Topic: Forcekill  (Read 3930 times)

I whipped up this forcekill real quick and I wanted to test it. The syntax is all correct, BUT, the command doesn't do anything.
I feel that it's not actually finding the player.

Code: [Select]
function Kill(%client, %target)
{
if(%client.isSuperAdmin)
{
findClientByName(%target).player.kill();
echo("success");
}
}

Your function should work just fine, I'm thinking you might have been calling it wrong. You can't say "/Kill blah" in chat to kill a player using this, for that you have to change the function name to "serverCmdKill" instead of just "Kill"

Your function should work just fine, I'm thinking you might have been calling it wrong. You can't say "/Kill blah" in chat to kill a player using this, for that you have to change the function name to "serverCmdKill" instead of just "Kill"
oh wow, i'm dumb

So I fixed it and added a messageAll(); line to it.
Now when I try to display the name of the user who got foreckilled, it's in lowercase instead of uppercase.

Code: [Select]
function serverCmdKill(%client, %target)
{
if(%client.isSuperAdmin)
{
findClientByName(%target).player.kill();
messageall('',"\c3" @ %target SPC "\c6has been forcekilled.");
}
}

EDIT: I figured out it's actually displaying the input that was given.

findclientbyname(%name).getPlayerName()

Better also add a check to see if their player exists first so you don't get an error or send out an unnecessary chat message if they're already dead.

check if the player object exists too, otherwise console will return an error:
Edit: Also, getting the full players name in the message all function is more efficient :p
Edit x2: also notifying client if player doesnt exist

Code: [Select]
function serverCmdKill(%client, %target)
{
if(%client.isSuperAdmin)
{
                        %player = findClientByName(%target).player;

                        if(isObject(%player))
                        {
messageall('',"\c3" @ %player.client.name SPC "\c6has been forcekilled.");
%player.kill();
                        }
                        else
                            messageclient(%client,'',"\c6Player not found.");
}
}


sorry for being so overly complicated, its in my nature lol
« Last Edit: December 28, 2014, 07:00:22 AM by General »

Edit: Also, getting the full players name in the message all function is more efficient :p
More efficient than what?

also, fixed name reference to preferred method, and crazy indenting
Code: [Select]
function serverCmdKill(%client, %target)
{
if(%client.isSuperAdmin)
{
%player = findClientByName(%target).player;

if(isObject(%player))
{
messageall('',"\c3" @ %player.client.getPlayerName() SPC "\c6has been forcekilled.");
%player.kill();
}
else
messageclient(%client,'',"\c6Player not found.");
}
}


At this point it'd probably be nicer to instead keep the actual result of findClientByName and then get the name as %client.getPlayerName(). This would also allow for some simpler differentiating between "client not found" and "client has no player".

This would also allow for some simpler differentiating between "client not found" and "client has no player".
I thought about that but didn't think of this point
I think OP knows enough to do this himself, rather than spoonfeeding.
Also, I'd restructure the code to a series of if(condition false) return rather than a bunch of nested if(condition true) { if(condition true) { ... etc
Just looks nicer IMO


You are aware the method I used also works right?
Yeah but it's obsolete and getPlayerName() should be used instead

Quote from: v10 to 11 changelog
Client name is now accessed through %client.getPlayerName() (%client.name is still provided for backward compatibility but %client.getPlayerName() is preferred)
« Last Edit: December 28, 2014, 01:19:13 PM by Headcrab Zombie »

How is %client.getPlayerName()  superior to %client.name?

Short answer: Because Badspot says it is

Long answer: Whenever you have a variable and a method that point to the same thing, it's (usually) better practice to use the method rather than the variable. Similarly, although %obj.position is a valid field that contains the objects position, you never see it used, instead you see %obj.getPosition().
In languages that support it, you'll usually define the variable as private (only accessible within its class) or protected (only usable with its class or derived classes) and then create a public (usable wherever) method to access that variable

The more you know. All my mods read the variable, ah well