Making a sword hurt a part based on team

Hey guys.

How do I make a script where I can assign a model or part x amount of health and assign it a team, so that people on the opposite team can harm it with a weapon? the problem im having is that i can’t figure out how to filter out the damage between players on different teams, and it ends up hurting the humanoid inside the part/model regardless of team.

Here is my code:

image

image

I would very much appreciate if anyone could help me with this. Right now in the code it recognizes if they part is touched, and deals damage based on the team. But what im trying to do is to get the sword to hurt the part only if you’re on the opposite team. Can anyone help me?

You should add a StringValue and assign the value to the team color, then check the players team and the statues team. If they are different, then deal damage.

1 Like

but how would i work this out? how do i actually assign this into the humanoid?

Is BaseColor a Color3Value or a BrickColorValue? It should be a BrickColorValue. You could also try using an ObjectValue containing the Team Instance instead.

but how do I actually code this? how do i input this into the humanoid?

If you have an ObjectValue called ‘Team’ containing the correct team object, maybe this could work.

local Players = game:GetService("Players")

local function onTouched(otherPart)
    if RedBase.Health == 0 then
        return
    end
    local partParent = otherPart.Parent
    if partParent.Name == "ClassicSword" then
        local plr = Players:GetPlayerFromCharacter(partParent.Parent)
        if plr and plr.Team ~= RedBase.Team.Value then
            RedBase:TakeDamage(Damage)
        end
    end
end
Head.Touched:Connect(onTouched)

I think, there is a function for teams, like game:GetService("Teams").TeamRed:GetPlayers and this will return a table with all players in that team.

Only kills the red team

local kill = script.Parent

kill.Touched:Connect(function(hit)

local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then
if player.Team == game.Teams.Red then	
local humanoid = hit.Parent:FindFirstChild("Humanoid")	
	if humanoid then
		
	humanoid.Health	= 0
		
	end	
end
end	

end)

This sword still hurts the humanoid even though we are on the same team. This script goes into the “Head” part correct? The sword i am using is roblox’s classic sword.

RedBase:TakeDamage(damage) also “damage” is redlined, i’m not sure where it is supposed to get “damage” from.

The function is supposed to replace the onPartTouched function in the script you posted. damage should actually be Damage, as that’s the damage variable in your original script. I have edited that in the code now.

You also mentioned that it hurts even though you are in the same team. What is shown in the output if you add this?

print(plr.Team, RedBase.Team.Value)

the damage was just a test, i was trying to hurt the humanoid just by touching it but i actually want the damage to come from the sword itself. I think the sword scripts might need to be edited to control what it can damage, but i’m not sure how to do that.

The touched function I posted checks if the part that hits the ‘Head’ is parented to a sword.

If you want to control damage from the script that controls the sword, then do you want to mostly keep the sword’s default behavior and only change what humanoids it damages? Or do you want the sword to do damage once per attack? If you want simple behavior, it might be a good idea to just replace the sword script with your own one.

yes, i would. The idea was that i can just edit the classic linked sword so that it hurts other players who are not on your team and also the same thing but with a part that has a team assigned to it. I am not a good scripter so I wouldn’t be able to just write my own sword.

A sword that kills other players who are not on your team

1 Like

You can use :GetPlayerFromCharacter(touchedpart) in order to get the player whose character was hit, and then check if their team is the opposite off of the person who hit the player. For example:

Sword.Blade.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then --check if it's a player's character or it has a humanoid to damage
        local playerHit = game.Players:GetPlayerFromCharacter(hit.Parent);
        if playerHit then --check if player exists to prevent errors
            if playerHit.Team ~= game.Players:GetPlayerFromCharacter(Sword.Parent).Team then
               hit.Parent.Humanoid:TakeDamage(damage)
           end
        end
    end
end)

If this doesn’t work, you may want to involve the names of the teams now.

1 Like

I noticed you has posted the sword code in another topic.

Try replacing the original blow function with this and make sure that an ObjectValue containing the correct team is parented to the humanoid.

function Blow(Hit)
	if not Hit or not Hit.Parent or not CheckIfAlive() then
		return
	end
	local RightArm = (Character:FindFirstChild("Right Arm") or Character:FindFirstChild("RightHand"))
	if not RightArm then
		return
	end
	local RightGrip = RightArm:FindFirstChild("RightGrip")
	if not RightGrip or (RightGrip.Part0 ~= RightArm and RightGrip.Part1 ~= RightArm) or (RightGrip.Part0 ~= Handle and RightGrip.Part1 ~= Handle) then
		return
	end
	local character = Hit.Parent
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if not humanoid then
		return
	end
	local player = Players:GetPlayerFromCharacter(character)
	if player then then
		if player.Team ~= Player.Team then
			return
		end
	else local teamObjVal = humanoid:FindFirstChild("Team")
		if not teamObjVal or teamObjVal.Value ~= Player.Team then
			return
		end
	end
	UntagHumanoid(humanoid)
	TagHumanoid(humanoid, Player)
	humanoid:TakeDamage(Damage)
end
1 Like

this is EXACTLY what i’m talking about, but that sword appears to be killing actual players. I need those players to be npc’s though. Do you understand what i mean?

That would be fantastic.

-- example
local Player = script.Parent.Parent

script.Parent.Touched:Connect(function(hit)
    local hum = hit.Parent or hit.Parent.Parent
    if hum and hit.Parent.Parent.Team or hit.Parent.Parent.Parent.Team ~= Player.Team then
        hum:TakeDamage(math.random(10,20))
    else
        print("Same Team")
    end
end)

Yes I know the touched function is not very good for this, but I’m not really good at scripting so you may need to look at other peoples hit functions

1 Like

what is your native language?
chars

انا عربي
استخدم قوقل للترجمة
I am an Arab
Use google to translate