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

Hit function not recognizing players body parts?

Im trying to create a test tool that destroys another players body part when touched yet its not working and not giving me any errors, Any ideas?

local p = game.Players.LocalPlayer
local c = p.Character
local x = {"Right Arm", "Right Leg", "Torso", "Left Arm", "Left Leg", "Head"}
script.Parent.Touched:connect(function(hit)
	if hit.Parent ~= c then return end
		if hit.Parent == x then
			x:Destroy()
		
	end
end)

2 comments

0

Is FilteringEnabled active?

0

Yes

1 answer

✓ Accepted answer

You're actually missing an end on the innermost if statement. Also, your variable names aren't very descriptive. hit.Parent will never == x either, because x is a table. "connect" is deprecated, you should use Connect instead. :)

Instead, I recommend on doing this in a server-script inside game.ServerScriptService:

local part = workspace.Part
local debounce = true

local childrenToDestroy = {"Left Arm"}

part.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)

	if player then
		for _, characterChild in pairs(player.Character:GetChildren()) do
			for _, targetChild in pairs(childrenToDestroy) do
				if characterChild.Name == targetChild then
					characterChild:Destroy()
				end
			end
		end
	end
end)

I'm assuming this is what you're trying to do but I'm not quite sure.

1 comment

0

Thanks this helped alot

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