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

How can I detect hits while moving something with CFrame?

Asked by 3 years ago

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

Log in to vote
0
Answered by
sifn 70
3 years ago
Edited 3 years ago

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)
Ad

Answer this question