If a part a specific name touches the scripts parent. Something happends

Hello,

I am trying to make a light change by a vehicle, this is what i need:

If a part with a specific name, touches another part (The script’s parent.), something will change.
Keep in mind. the part with the specific name are in multiple models.

Sincerely,
Milk.

Use BasePart’s Touched event. Sample code:

Part.Touched:Connect(function(touchingPart)
	if touchingPart.Name == "DESIRED NAME" then
		--Run logic
	end
end)

Works, thank you for your help!

local part_script = script.Parent -- the script's parent part
local part_other_name = string -- all parts with this name will be detected

part_script.Touched:Connect(function(part_incoming)
	if part_incoming.Name == part_other_name then
		print("Part touched", part_incoming)
	end
end)

If you want to go a step further in making sure only selected parts can touch, you could either use Attributes or CollectionService to give attributes / tags to specific objects (parts in this case) only.

-- Attributes
local part_script = script.Parent -- the script's parent part
local part_other_attribute = string -- all parts with this attribute set to true will be detected

part_script.Touched:Connect(function(part_incoming)
	if part_incoming:GetAttribute(part_other_attribute) then
		print("Part touched", part_incoming)
	end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.