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
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
cweego
(cweego)
July 21, 2021, 9:30pm
#4
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
D0RYU
(nici)
July 21, 2021, 9:35pm
#6
I think :FindFirstChildOfClass(“Humanoid”) or :FindFirstChildWhichIsA(“Humanoid”) would be slightly safer to use
cweego
(cweego)
July 21, 2021, 9:40pm
#7
It really depends on what @9815464308js wants. If he doesn’t want it to affect NPCs, @hugecoolboy2007 ’s way would be better.
D0RYU
(nici)
July 21, 2021, 9:42pm
#8
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
cweego
(cweego)
July 21, 2021, 9:44pm
#9
My bad, I now see what you meant. I thought you were saying that using :GetPlayerFromCharacter()
was bad, because apparently I can’t read.