Jon's Mini Tutorials [1]

Hey there! I’m going to be sharing some neat tricks I’ve learned (and how to implement them) over the near 6 years I’ve been on ROBLOX.

[center][size=4]How to harm based on team[/size]
I chose this for the first one because not everyone here scripts, and there’s a good chance clanners fall into that area. Having experience with clans, I was tasked with creating a sword that would only harm people on a different team.

While it can set a variable for this, you’d then have to make one sword per team. So instead, I made one that checked the attackers team against the attackees (?) team.

Please note I am modifying the standard LinkedSword script. [/center]

[spoiler][code]
function blow(hit)
if (hit.Parent == nil) then return end – happens when bullet hits sword

local humanoid = hit.Parent:findFirstChild(“Humanoid”)
local vCharacter = Tool.Parent
local vPlayer = game.Players:playerFromCharacter(vCharacter)
local hum = vCharacter:findFirstChild(“Humanoid”) – non-nil if tool held by a character
if humanoid~=nil and humanoid ~= hum and hum ~= nil then
– final check, make sure sword is in-hand

  local right_arm = vCharacter:FindFirstChild("Right Arm")
  if (right_arm ~= nil) then
     local joint = right_arm:FindFirstChild("RightGrip")
     if (joint ~= nil and (joint.Part0 == sword or joint.Part1 == sword)) then
        tagHumanoid(humanoid, vPlayer)
        humanoid:TakeDamage(damage)
        wait(1)
        untagHumanoid(humanoid)
     end
  end

end
end
[/code][/spoiler][center]

Now, to make this easier on me, I’m going to clean that up a bit…[/center]

[spoiler][code]
function blow(hit)
if not hit.Parent then return end

if type(hit.Parent) == ‘Model’ then – in case we hit a hat
local hHum = hit.Parent.Parent:FindFirstChild(‘Humanoid’)
else
local hHum = hit.Parent:FindFirstChild(‘Humanoid’)
end

local vC = Tool.Parent
local vP = game.Players:GetPlayerFromCharacter(vC)
local hP = game.Players:GetPlayerFromCharacter(hHum.Parent)
local vHum = vCharacter:findFirstChild(“Humanoid”)
if hHum and hHum ~= vHum and vHum then
tagHumanoid(humanoid, vP)
humanoid:TakeDamage(damage)
wait(1)
untagHumanoid(hHum)
end
end
[/code][/spoiler]

[center]Now, we need to add a check comparing the two players teams!
It’s as simple as this:[/center]

if vP.TeamColor ~= hP.TeamColor then

[center]Final Code:

[spoiler][code]
function blow(hit)
if not hit.Parent then return end

if type(hit.Parent) == ‘Model’ then – in case we hit a hat
local hHum = hit.Parent.Parent:FindFirstChild(‘Humanoid’)
else
local hHum = hit.Parent:FindFirstChild(‘Humanoid’)
end

local vC = Tool.Parent
local vP = game.Players:GetPlayerFromCharacter(vC)
local hP = game.Players:GetPlayerFromCharacter(hHum.Parent)
local vHum = vCharacter:findFirstChild(“Humanoid”)
if hHum and hHum ~= vHum and vHum then
if vP.TeamColor ~= hP.TeamColor then
tagHumanoid(humanoid, vP)
humanoid:TakeDamage(damage)
wait(1)
untagHumanoid(hHum)
end
end
end
[/code][/spoiler][/center]

3 Likes

You also need to check Neutral since players are on the same team color by default.

EDIT: and also check if the target player is nil since NPCs have no owner player.

[quote] You also need to check Neutral since players are on the same team color by default.

EDIT: and also check if the target player is nil since NPCs have no owner player. [/quote]

I didn’t put those in because in the majority of clan places, you don’t have NPCs and always have teams.

This tutorial is misleading then. I don’t want to call you out or anything but you’re not teaching people what you’re saying you’re teaching them. That’s not cool yo.

I’m confused.

You’re saying that this script will harm players on enemy teams. You should make it very clear that your script breaks if you’re attacking an NPC and that it doesn’t work if both players are neutral.

Ok, you’re right in the term of checking if the player is nil. I normally add that, but for whatever reason I forgot to add that. I thought you meant checking if the player is actually on a team.

As for the neutral, I just sort of assumed you have at least one Auto-Assign team. I’ll amend that some time tomorrow with the proper code as I’m on my phone and it’s 11:30 at night.