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

I want this to only affect players, but it affects all parts. What should I do?

Asked by 6 years ago

I want this to only affect players, but it affects all parts. What should I do?

function onTouched(Humanoid)


Humanoid:BreakJoints (0)

end


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

0
BreakJoints doesn't have any arguments. Also, Humanoid is a bad parameter name, as it is not a humanoid. Use GetPlayerFromCharacter. hiimgoodpack 2009 — 6y
0
What do you mean exactly? TheBeaver101 28 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

Hi Beaver,

What you're trying to accomplish is quite simple. You just need to check if the part that it touched is a character's part, which can easily be done by checking if the model has a humanoid. Then, when you know it's a character, you just use the :BreakJoints() method on the character. Here, I'll show you how to do it:

function onTouched(obj)
    if obj.Parent:FindFirstChild("Humanoid") then
        local char = obj.Parent;
        char:BreakJoints()
    end
end

script.Parent.Touched:Connect(onTouched);

Well, I hope I helped and have a wonderful day/night.

Thanks,

Best regards,

~~ KingLoneCat

0
Thank you so much, good lord. It finally works. TheBeaver101 28 — 6y
0
np KingLoneCat 2642 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

So, you would need to add a check. Your script would break any joints that a part would come in contact with without checking if it is a player. You can prevent this with an if statement and a FindFirstChild.

function onTouched(hit)
    if hit.Parent:FindFirstChild("Humanoid")  and game.Players:GetPlayerFromCharacter(hit.Parent)then
        hit.Parent:BreakJoints()
    end

end


script.Parent.Touched:connect(onTouched)

Now, in case you don't know, an if statement would evaluate a condition. If the condition is true, it would execute the code in between the statement and end. The FindFirstChild means that it would get all of the children of the model (which would be the parent of the part that touches the part in this case) and find a part named Humanoid in this case. And if there's a humanoid found, then the if statement would evaluate to true and kill the player.

There's also an optional thing you can do. It does work, but say for instance, an npc would touch it. It'd still die. If you don't want it affecting npcs, you would change your code to this.

function onTouched(hit)
    if hit.Parent:FindFirstChild("Humanoid")  and game.Players:GetPlayerFromCharacter(hit.Parent)then
        hit.Parent:BreakJoints()
    end

end


script.Parent.Touched:connect(onTouched)

:GetPlayerFromCharacter would evaluate if the model that touched it is actually a player's character.

So now, the script would BOTH evaluate if there's a humanoid present in the part of it and if the model that touched it is a player.

0
Thank you so much! TheBeaver101 28 — 6y

Answer this question