How to make a repeat until stop when it touches something?

I want to make a repeat until loop until my bullet touches something. Here is my script so far `local function Ontrigger()
local bullet = script.Parent.Bullet:Clone()
local rotationz = bullet.Rotation.Z
local roationy = bullet.Rotation.Y
local rotationx = bullet.Rotation.X

bullet.Transparency = 0
bullet.Parent = game.Workspace
bullet.WeldConstraint:Destroy()
bullet.LinearVelocity.Enabled = true
repeat
	print("not false")
	bullet.Rotation = Vector3.new(rotationx,roationy,rotationz)
	wait(0.05)
until *My bullet touches something*

end

script.Parent.Activated:Connect(Ontrigger)`

how would I do this?
any feedback appreciated!!!

oh and a way that allows me to check what is being touched as well would be good!

Connect your bullet to a simple .Touched event before you start your repeat loop.

local touchedSomething = nil

bullet.Touched:connect(function(hit)
  touchedSomething = hit
end)

Now you can just make your repeat loop end at:

until touchedSomething
1 Like

Do not use .Touched, it’s very bad.
Instead, you should use :GetTouchingParts every X amount of time. This is a lot more reliable and also means you don’t have to worry about cleaning up events you make, which is a plus.

local RunService = game:GetService("RunService")
local Heartbeat = RunService.Heartbeat

local Hit

--//

while true do
    Hit = bullet:GetTouchingParts()[1]

    if Hit then
        break
    end

    Heartbeat:Wait() -- Wait 1 frame.
end
1 Like

If it’s something to consider, using a module like FastCast makes life easier. It uses raycasting which is very performant, allows you to render bullets (runs in Heartbeat), has bullet physics, and gives you full control of the bullet (you can pause, resume, accelerate, and etc.).

You’re absolutely right. I’ve never touched the module myself, but I know a lot of games have used it and it is made well.