Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to detect if a brick touches a brick?

Asked by 6 years ago

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)
0
I don't think thats how detector works however you can surely see the magnitude and when its = 0 then the object touches l_337 15 — 6y
0
Are you able to make it so that magnitude works with every brick in the game? MyTradeJustWentViral 5 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

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)
Ad
Log in to vote
0
Answered by
zblox164 531 Moderation Voter
6 years ago

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.

Answer this question