There are many situations where you don't wish for players to be able to kill their teammates, and this behaviour is easy to accomplish through 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 on studio. If you wish to know more about the toolbars available to you in Studio 2013 then click here. Please note that the information provided, at that page, is only relevant to users using the 'RibbonBar' layout.
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. If you still cannot find it, it's usually best to ask for help on the Scripting Helpers forum.
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.
We will be preventing players from teamkilling by comparing the striked 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 LocalPlayer property of the Players service (LocalScripts only), or by use of the 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 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.