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

How could I check is a part being touched?

Asked by
k5a15 2
3 years ago

I would like to make a button that would turn red when clicked when the detector part is being touched and otherwise turn green. I had tried the code below and it turnouts only showing red if a part is starting to touch but not touching. (e.g. if you walk into the part then click the button, it still shows green. If you drive a car slowly into the detector, since parts at the rear of the car is entering the block, it would turn red on click.)

script.Parent.ClickDetector.MouseClick:connect(function()
    print("Button")
    local Parts = script.Parent.Parent.Detect:GetTouchingParts()
    if #Parts == 0 then
        script.Parent.BrickColor = BrickColor.new("Lime green")
    else
        script.Parent.BrickColor = BrickColor.new("Really red")
    end
end)

1 answer

Log in to vote
0
Answered by
174gb 290 Moderation Voter
3 years ago
Edited 3 years ago

Probably a another part like the ground is touching the 'Detect' part, you should change 0 to 1 or whatever, tested and worked here GIF

Here's my code

local function GetTouchingParts(part)
    local connection = part.Touched:Connect(function() end)
    local results = part:GetTouchingParts()
    connection:Disconnect()
    return results
end


script.Parent.ClickDetector.MouseClick:connect(function()
    local Parts = GetTouchingParts(script.Parent.Parent.Detect)
    if #Parts == 1 then
        script.Parent.BrickColor = BrickColor.new("Lime green")
    else
        script.Parent.BrickColor = BrickColor.new("Really red")
    end
end)
Ad

Answer this question