Making a sword hurt a part based on team

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

Did you download the sword
You want to make NPC a team and the sword only kills the other team NPC, or you want to kill the players too

حسنًا ، أريد فريقين في لعبتي وسيكون لديهم ترسانة من الأسلحة ، ولحل هذه المشكلة سنستخدم سيفًا عاديًا. أرغب في تعيين فريق لجزء أو نموذج ، وللقيام بذلك كنت سأستخدم قيم اللون البشري والطوب لمطابقة قيم ألوان الفريق في اللعبة. إذا قام اللاعبون الموجودون في نفس الفريق الذي ينتمي إليه الجزء بضربه بالسيف ، فلن يحدث أي ضرر. إذا قام لاعب ليس من فريق الجزء بضربه ، فإن الجزء يتعرض للضرر.

Holy cow! this worked! but there are a few problems:

  • For some reason the teams are swapped but i can probably just do this myself. I can only damage the part when im on the same team, but not on the enemy team? Nevermind I fixed this

  • The hitbox is god awful

  • Will this work with explosive weapons? I wanted to have the sword coded first but it seems every weapon will have to be coded individually, any ideas?

I don’t know how the explosive weapons you use do the damage. If the damage is done by the explosion, then the way it’s done probably needs to be changed. If it’s done be directly setting health or using TakeDamage, then just adding the same checks should work.

The explosive weapons i was using are roblox’s classic ticking time bomb, and the classic rocket launcher.

A player’s sword and a player’s NPC change with the player’s team

The file contains all the details in the video
Team change sword and NPC.rbxl (40.4 KB)

Do not hesitate to ask any questions. I love to learn and help others
If there are errors, I am glad you mentioned them in the comments

1 Like