"Touched" event breaking due to a "while" loop., need a replacement that does NOT break it

for i,v in pairs(script.Parent:GetChildren()) do
	if v:IsA("Part") then
		v.Touched:Connect(function(hit)
			onHit(v,hit)
		end)
		while task.wait(0.1) do
			(blablabla code here)
		end
	end
end

the “blablabla code here” is just for show, no idea why i even changed it to that in the post. oh well, whatever

This approach should prevent your Touched event from breaking and allow both the event handling and the loop logic to run smoothly. It’ll also avoid potential memory leaks associated with your while loop.

local activeLoops = {}

local function startLoop(part)
    local loopActive = true
    table.insert(activeLoops, {part = part, active = loopActive})
    
    while loopActive do
        task.wait(0.1)
        -- Your loop logic here (blablabla code)
    end
end

local function cleanup(part)
    -- Find and deactivate the loop for the given part
    for i, loop in ipairs(activeLoops) do
        if loop.part == part then
            loop.active = false
            table.remove(activeLoops, i)
            break
        end
    end
end

for i, v in pairs(script.Parent:GetChildren()) do
    if v:IsA("Part") then
        local touchConnection = v.Touched:Connect(function(hit)
            onHit(v, hit)
        end)
        
        task.spawn(function()
            startLoop(v)
        end)

        -- Cleanup connection and loop when the part is removed
        v.AncestryChanged:Connect(function(_, parent)
            if not parent then
                touchConnection:Disconnect()
                cleanup(v)
            end
        end)
    end
end

can i stop getting ai generated code
this may not be ai generated but there are several things that give it away
i’ll test it, if it works, thanks

looked at the code, this is literally what i DIDNT want. a while wait loop (not exactly, its a while true, still has a wait inside)

Then you should’ve specified that more clearly in your initial post. You can try using RunService’s HeartBeat event instead, if this is what you would like.

While loops must have an exit condition (or due to lua’s synchronous nature, a wait)

RunService can run code every server tick, or frame? if that’s what you’re looking for.

using heartbeat in a for i,v would not be very performant i believe
but thinking about it, neither would a while loop lol. i’ll try

This isn’t necessarily true, it’s about the implementation. Currently your for-loop never exits because you have a while-loop inside of it, with no exit condition. This also leaves you prone to memory leaks, which will cause lag over time.

If you want to avoid while-loops entirely, I would try RunService and iterating over a table of hit parts. Though, it should be known that while Heartbeat is an event being fired, it’s basically acting like your while-loop.

Something along the lines of:

local RunService = game:GetService('RunService')
local hitParts = {}

local function processTouchedParts()
     for _, part in pairs(hitParts) do
          onHit(part.part, part.hit) -- Your onHit function will need to remove the object from hitParts once it's finished, otherwise this function will trigger forever.
      end
end

for i,v in pairs(script.Parent:GetChildren()) do
	if v:IsA("Part") then
		v.Touched:Connect(function(hit)
			table.insert(hitParts, {v, hit})
		end)
        end
end

RunService.Heartbeat:Connect(processTouchedParts)