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)
Hi Beaver,
: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);
Thanks,
Best regards,
~~ KingLoneCat
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.