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

I need to detect body parts, not accessories. Help?

I can't seem to find a way to detect R6 bodyparts. Help?

ToolDetector.Touched:Connect(function(hit)
	if hit.Parent.Name == "Left Leg" or "Right Leg" or "Torso" or "Right Arm" or "Left Arm" or "Head" then
		print(hit.Parent.Name.. " has touched the metal detector.")
		playerCharacter = hit.Parent
		hitPlayer = game.Players:FindFirstChild(hit.Parent.Name)
		loggedPlayer = game.Players:FindFirstChild(LoggedUser.Value)
 		loggedGui = loggedPlayer.PlayerGui:FindFirstChild("MetalDetectorGui")
		playerBackpack = game.Players:FindFirstChild(hit.Parent.Name).Backpack
	
		loggedGui.username.Text = playerCharacter.Name
		for i, v in pairs(playerBackpack:GetChildren()) do	 
			print(v)
   		   loggedGui.items.Text = "backpack: "..v..""
		end
	else
	end 
end)

The Whole Script:

--Settings
GroupID = 4994106
minimunRank = 0 -- Get the number of your role and subtract it by one.

-- ** Do not edit anything else further on. **
Body = {"Left Leg", "Right Leg", "Torso", "Right Arm", "Left Arm", "Head"}
Screen = script.Parent.Parent.Screen
ScreenClickDetector = Screen.ClickDetector
LoggedUser = script.Parent.LoggedUser
groupRank = ""
ScreenGui = script.Parent.MetalDetectorGui
ToolDetector = script.Parent.Parent.ToolDetector

ScreenClickDetector.MouseClick:Connect(function(player)
	groupRank = player:GetRankInGroup(GroupID)
	
	if groupRank <= minimunRank then
		print("'"..player.Name.."' has Attempted to log into a metal detector.")
	else
		local ScreenClone = ScreenGui:Clone()
		ScreenClone.Parent = player.PlayerGui
		LoggedUser.Value = player.Name
	end
end)

ToolDetector.Touched:Connect(function(hit)
	if hit.Parent.Name == "Left Leg" or "Right Leg" or "Torso" or "Right Arm" or "Left Arm" or "Head" then
		print(hit.Parent.Name.. " has touched the metal detector.")
		playerCharacter = hit.Parent
		hitPlayer = game.Players:FindFirstChild(hit.Parent.Name)
		loggedPlayer = game.Players:FindFirstChild(LoggedUser.Value)
 		loggedGui = loggedPlayer.PlayerGui:FindFirstChild("MetalDetectorGui")
		playerBackpack = game.Players:FindFirstChild(hit.Parent.Name).Backpack
	
		loggedGui.username.Text = playerCharacter.Name
		for i, v in pairs(playerBackpack:GetChildren()) do	 
			print(v)
   		   loggedGui.items.Text = "backpack: "..v..""
		end
	else
	end 
end)

2 comments

1

You're checking the hit.Parents name, that would be the character. You have to check the hit's Name. hit.Name == "Left Leg", you could also use if hit:IsA("Part") then etc. That will check if hit is a part, not an accessory.

0

Thanks very much!

2 answers

✓ Accepted answer

The reason your script isn't working is because you are checking if the hit's parent's name is equal to that of a body part. If it's a body part, it will only get the name of the actual Character.

welcome to the cringe-fest

ToolDetector.Touched:Connect(function(hit)
    -- simply check if the hit part is a valid child of a character model
    -- (this won't work for accessories, because accessories don't have humanoids inside of them)
    if hit.Parent:FindFirstChildOfClass("Humanoid") then
        -- .. do stuff
    end 
end)

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