Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

2 comments

0

BreakJoints doesn't have any arguments. Also, Humanoid is a bad parameter name, as it is not a humanoid. Use GetPlayerFromCharacter.

0

What do you mean exactly?

2 answers

✓ Accepted answer

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

2 comments

0

Thank you so much, good lord. It finally works.

0

np

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.

1 comment

0

Thank you so much!

This discussion is preserved as it was. New answers, comments, and votes are closed.