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

How can I detect hits while moving something with CFrame?

Im using lookvector and I want it to keep going until its hit. Im using Script.Parent.Touched and stuff but it doesn't work and I don't want to have to use tweening and such to move it.

1 answer

✓ Accepted answer

There is a function that may suit your needs here, commonly used for touch detection. I present you with: :GetTouchingParts().

local touchingParts = script.Parent:GetTouchingParts()

It returns an array containing all of the parts that are touching what script.Parent is. From there, it is just a matter of looping through it.

for _,otherPart in ipairs(touchingParts) do
	print(otherPart.Name) -- If this part is found to belong to a player's character, you can damage it!
end

However for your use-case, you want to destroy the part immediately upon contact with another part. You can do this by checking if there is anything at all in the returned array.

Final script:

game:GetService("RunService").Heartbeat:Connect(function()
	local touchingParts = script.Parent:GetTouchingParts()
	if #touchingParts > 0 then
		script.Parent:Destroy()
	end
end)

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