Scripts run multiple times without having a loop

Help, for some reason all the scripts in my place that do not have a wait() are executed multiple times at the start. Even if I have a check before deleting something, it gives me an error that it does not exist (it has already been deleted) as if it were a loop. But none of the scripts that give this error have any loop. I realized that they are executed many times because when I add a print, the print appears many times, as seen in the image. Why does this happen?

image
image

Well touch events execute as long as you are touching the part, not just once.

Multiple parts in the character are touching soundPart1 when the character walks in/on it, so the event is fired multiple times.

Since the function destroys a part, I’m guessing this part is not supposed to be touched again (by this specific player, at least). In that case, you can disconnect the connection completely after it has fired and all conditions are met.

local touchedConnection
touchedConnection = soundPart1.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player and player == game.Players.LocalPlayer then
        touchedConnection:Disconnect()
        -- Rest of code
    end
end)
1 Like

You’re right, by removing the part at the end of the script, the error stops appearing, thank you.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.