Sword That Cuts Players But Not Parts

Hello! I made a sword that will delete the part of the player it touches. But whenever it touches a unanchored part that’s not the player (its a car) it will break it. Is their any way to fix this? Here’s my script.

function onTouched(hit)

if (hit.Name == "Safe") then return 

else
hit:BreakJoints()

end

end

connection = script.Parent.Touched:connect(onTouched)

Or is this the cars fault since the car is unanchored. Thank you :smiley:

You could check if a part belongs to a player beforehand:

if game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) then
 -- if this returns true then the part belongs to the player's character
else
 -- if it returns false, it doesn't belong to a player
end
1 Like

It should check to make sure what it’s hitting is part of a character with this code:

if hit.Parent:FindFirstChild("Humanoid") then
    -- Whatever is supposed to happen if the sword hits a player character here
end
1 Like

I just want to add that @hugecoolboy2007’s way would make it affect only players and not NPCs and stuff. @ZoomSummit’s way would affect all humanoids.

3 Likes

I belive its just the cars issue so I’ll try making a different car but tysm.

1 Like

I think :FindFirstChildOfClass(“Humanoid”) or :FindFirstChildWhichIsA(“Humanoid”) would be slightly safer to use

It really depends on what @9815464308js wants. If he doesn’t want it to affect NPCs, @hugecoolboy2007’s way would be better.

yea I agree, just saying because then you can name the Humanoid Instance to whatever you want due to the class being named Humanoid

which is why I said it was safer then :FindFirstChild() for this situation

1 Like

My bad, I now see what you meant. I thought you were saying that using :GetPlayerFromCharacter() was bad, because apparently I can’t read.