How do i make so part only detects a certain part from player

So iI wanted to create a part that has can collide set to true only if the left or right arm is detected from the player , if any other part like leg or head contacts with the part can collide is set to false.
I tried this script but it didn’t work:


local function OnTouched(Object)
	if game.Players:GetPlayerFromCharacter(Object["Left Arm"]) or game.Players:GetPlayerFromCharacter(Object["Right Arm"]) or game.Players:GetPlayerFromCharacter(Object["Left Leg"]) or game.Players:GetPlayerFromCharacter(Object["Right Leg"]) then
		script.Parent.CanCollide = true
	end
end

Part.Touched:Connect(OnTouched)

so the object is touching the part if you want to make sure the object touching the part is an arm

check
if object.Name == “Arm” then
-----print(“touched an arm”)
end

should work sumn like that

local function OnTouched(Object)
	if game.Players:GetPlayerFromCharacter(Object.Parent) then
		for i, v in pairs({"Left Arm", "Right Arm", "Left Leg", "Right Leg"}) do
			if Object.Name == v then
				script.Parent.CanCollide = true
			end
		end
	end
end

Part.Touched:Connect(OnTouched)
local Players = game:GetService("Players")

local Part = script.Parent

local function OnTouched(Object)
	if Object.Name:match("Arm$") or Object.Name:match("Leg$") then
		local Player = Players:GetPlayerFromCharacter(Object.Parent)
		if Player then
			print(Player.Name)
		end
	end
end

Part.Touched:Connect(OnTouched)

it worked but then when my arm touches the part it doesnt hang on to it , it just falls off

Can you provide the script you’re working with? The script I provided just prints the player’s name.


local Part = script.Parent

local function OnTouched(Object)
	if Object.Name:match("Arm$") then
		script.Parent.CanCollide = true
	else 
		script.Parent.CanCollide = false
		local Player = Players:GetPlayerFromCharacter(Object.Parent)
		if Player then
			print(Player.Name)
		end
	end
end

Part.Touched:Connect(OnTouched)

CanCollide just enables collisions for the part, I’m not exactly sure what you’re trying to achieve.

the part it doesnt hang on to it , it just falls off

Not sure what you mean by this.

i want it so that only the Legs or arm can touch the part , if some other player parts touches the part it shouldnt be able to

local Players = game:GetService("Players")

local Part = script.Parent

local function OnTouched(Object)
	if Object.Name:match("Arm$") or Object.Name:match("Leg$") then
		--Code here.
	else
		--Other code here.
	end
end

Part.Touched:Connect(OnTouched)

That’s what I already provided, the code here checks if the touching part is an arm/leg and then executes some code accordingly, you can add code inside the else expression if other code needs to be executed when the touching part is not an arm/leg.