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

How to detect if a brick touches a brick?

So I have a script that detects if another brick touches brick, I have the brick located in the players HumanoidRootPart. The problem with the script is that it also detects whenever your player touches it. I would just want to know if there is any way to make it so that it doesnt detect your own player touching it.

local plr = game.Players.LocalPlayer
local char = plr.Character
local HRP = char:WaitForChild("HumanoidRootPart")


--Creating the part

	local Detector = Instance.new("Part")
Detector.Position = Vector3.new(HRP.Position.X,HRP.Position.Y,HRP.Position.Z)
Detector.Name = "Detector"
Detector.Size = Vector3.new(5,2,5)
Detector.Anchored = false
Detector.Transparency = 0
Detector.CanCollide = true	
Detector.Parent = HRP

--Weld

	local Weld = Instance.new("Weld", Detector)
Weld.Part0 = HRP
Weld.Part1 = Detector


--Touched Function

Detector.Touched:Connect(function(hit)
	if hit.Parent.ClassName == "Part" then

		--Do stuff here
	
	end				
end)

2 comments

0

I don't think thats how detector works however you can surely see the magnitude and when its = 0 then the object touches

0

Are you able to make it so that magnitude works with every brick in the game?

2 answers

Use and hit.Parent ~= char so it ignores the player's character

--Touched Function

Detector.Touched:Connect(function(hit)
    if hit.Parent.ClassName == "Part" and hit.Parent ~= char then

        --Do stuff here

    end             
end)

The function IsA() should work

EXAMPLE:

Detector.Touched:connect(function(hit)
	if hit.Parent:IsA("Part") then
		-- Do stuff here
	end
end)

this should work This IsA function checks for a certain class and ignores everything else so that should be the most efficient way.

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