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

How can you detect if one part touches another part?

I am trying to make a 1+ leaderstats giver by making another part touch another part which when they collide with eachother you will get 1+ leaderstats and the block will disappear and I need to know how to detect if one part touches another, can someone help me?

2 answers

✓ Accepted answer

FIrst we should define the part that we specifically want to be touching the target. Let's pretend it's a ball that is going to touch a socket.

If the script is inside the part you are moving around, we can assume that it's going to touch a lot of things, which would make the script run over and over again, potentially causing lag. I would recommend running the script in the socket or target instead

local leaderstats = Instance.new("Folder", game.Workspace)
leaderstats.Name = "Contacts"

local points = Instance.new("IntValue", leaderstats)
points.Name = "Points"

local Target = script.Parent
local Ball = game.Workspace:WaitForChild("Ball")

local debounce = false -- make sure the code below only runs once when the part touches the target

Target.Touched:Connect(function(hit)
	if not debounce then -- make sure debounce is set to false
		debounce = true
		if hit == Ball then
			print("Ball has touched the part")
			Ball:Destroy() -- destroy the part
			points.Value += 1 -- increment the points by 1
		end
		debounce = false
	end
end)

You can use this to check when the part touches another part:

--make and connect a function for when a part is touched
script.Parent.Touched:connect(function(hit)

--put scripts regarding leaderStats here

--destroy the part
script.Parent:Destroy()
end)

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