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

How could I check is a part being touched?

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

✓ Accepted answer

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)

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