Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make player not die onTouch?

Asked by 7 years ago

I have a script that separates all of the parts but it kills the player. I want it to keep destroying the parts but I do not want it to kill the player. How do I make it so that the player is not killed in the process of the script?

function onTouched(hit)



hit:BreakJoints()



end







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

1 answer

Log in to vote
0
Answered by 7 years ago

Check if hit is a member of a humanoid.

You can do this by checking if the part's parent has a humanoid.

"How?"

Use an if statement!

First, make sure that the part exists. This is not necessary but I like doing so.

function onTouched(hit)
    if hit then
        hit:BreakJoints()
    end
end
script.Parent.Touched:connect(onTouched)

There.

Now we need to see if the "hit's" parent has a Humanoid. We can check both with one if statement by using the keyword and. Like so,

function onTouched(hit)
    if hit and hit.Parent:FindFirstChild("Humanoid") then--check both
        hit:BreakJoints()
    end
end
script.Parent.Touched:connect(onTouched)

The above only breaks the joints of things that don't have a humanoid. This would still break the joints of an NPC and such, but that shouldn't be a problem for you. If it is however,

Check for the hit's parent's name in Players.

Just add this to the if statement.

-- If you have NPC's.
function onTouched(hit)
    if hit and hit.Parent:FindFirstChild("Humanoid") and game.Players:FindFirstChild(hit.Parent.Name) then--check all
        hit:BreakJoints()
    end
end
script.Parent.Touched:connect(onTouched)

There's a slight chance that the NPC will have the same name as a player but we'll assume this problem doesn't exist as it's really rare.

That should work.

Hope that helps.

Good Luck!

0
What should the script's parent be? BunnyFilms1 297 — 7y
0
The part that's in workspace you want to monitor the touch event for. User#11440 120 — 7y
Ad

Answer this question