Disable Harming Teammates
Disable Harming Teammates
There are many situations where you don’t wish for players to be able to kill their teammates, and this behavior is easy to accomplish by modifying existing scripts. In this tutorial, we will be guiding you through the process of modifying Roblox’s most classic weapon, a sword. However, while the focus of this tutorial will be on one tool, the information provided here can be applied to any tool on Roblox.
The first thing that we’ll have to do, before we even start to modify a script, is insert a copy of Roblox’s “LinkedSword” from free models (or whichever tool you wish to modify). You can do this by opening the Toolbox and clicking over to the Search tab. In the field provided type in “LinkedSword” and press enter. You should click, and insert, the one which looks like Roblox’s “LinkedSword” (see figure 1 for reference).
If you cannot see the Toolbox make sure you have it enabled under the view tab in Roblox Studio.
Modifying the tool’s script
Tools can deal damage to a user’s humanoid in a number of ways, however, tools commonly achieve this through the use of the
BasePart/BreakJoints methods, or the humanoid’s
The ways to deal damage, as mentioned above, are commonly used from the tool’s main script, however, there are some tools which don’t do this. So, if you cannot find the code dealing the damage in the main script, try looking through any other scripts provided with the tool you’ve inserted.
In the case of the tool we’re using, a “LinkedSword”, the damaging portion of the code can be found in the main script, which is appropriately named “SwordScript” (see figure 2). To edit this we need to find it in the explorer pane and double click on it, or alternatively, we can select the item in the explorer window and press enter. Once you have done this you will be presented with Roblox’s script editor, but make sure you’ve opened the correct script.
Once you have opened the script, press key press|Ctrl|F together. This will open a window which is titled Find. We will be using this window to find the section of the script dealing damage. In the case of Roblox’s “LinkedSword,” we can simply search for TakeDamage. In doing this it will both highlight and take us to the section of code we need to modify (see figure 3).
We will be preventing players from teamkilling by comparing the stricken player’s TeamColor to the TeamColor of the player wielding the tool. The player wielding the LinkedSword is already defined in the variable
vPlayer in this instance. However, you can get the player wielding the tool (in any tool script) through the use of the
Players/LocalPlayer property of the Players service (LocalScripts only), or by use of the
Players/GetPlayerFromCharacter method (used on the tool’s parent).
In the case of the LinkedSword, we need to add any code, to prevent teamkilling, before the line using the tagHumanoid function, this is so that players aren’t wrongly given Knockouts for another player’s kill.
Below is the code we will be editing, in the LinkedSword’s script.
tagHumanoid(humanoid, vPlayer) humanoid:TakeDamage(damage) wait(1) untagHumanoid(humanoid)
We will change the code, shown above, to that shown below. This change will prevent teamkilling.
local Player = game.Players:GetPlayerFromCharacter(hit.Parent) if Player.TeamColor ~= vPlayer.TeamColor then tagHumanoid(humanoid, vPlayer) humanoid:TakeDamage(damage) wait(1) untagHumanoid(humanoid) end
The above modification is checking to make sure that the player who’s being hit doesn’t have the same
Player/TeamColor as the player who has the tool equipped. If the players don’t have matching TeamColors, the user is tagged and damaged. However, if the players do have matching TeamColors, nothing happens as the if-statement isn’t fulfilled.