Is there any way to break Repeat wait() loop?

I’m trying to break a repeat loop for a stun Timer system but i don’t know how

--//Services
local Run_Service = game:GetService("RunService")
local Replicated_Storage = game:GetService("ReplicatedStorage")

--//Tool
local Tool = script.Parent.Parent
local Sounds = Tool.Sounds:GetChildren()

--//Events
local Events_Folder = Tool.Events

local Damage_Remote = Events_Folder.DamageRemote

--//Values
local Stun_Time = 0
local Stun_Threshold = 0

--//Particles
local Combat = Replicated_Storage.Combat
local Gore = Combat.Gore

--| Main Script |--

function Update_Stun_Time()
	if Stun_Time >= 0 then
		repeat task.wait(0.1)
			print(Stun_Time)
			Stun_Time -= 1
		until Stun_Time <= 0
		if Stun_Time <= -0.1 then
			Stun_Time = 0
		end
end
end

function Damage_Target(Player, Target, Damage, Body_Part)
	local Target_Humanoid = Target.Humanoid
	local Stunned = Target:WaitForChild("Stun")

	Target_Humanoid.Health -= Damage
	Stun_Time = 6
	Tool.Sounds.AttackSound:Play()

	local Blood_Clone = Gore.Blood:Clone()
	Blood_Clone.Parent = Body_Part
	Blood_Clone:Emit(50)
	
	Stunned.Value = true
	print(Stunned.Value)
	
	if Stun_Time >= 0 then
		repeat task.wait(0.2)
			print(Stun_Time)
			Stun_Time -= 1
		until Stun_Time <= 0
		if Stun_Time <= 0 then
		Blood_Clone:Destroy()
		Stunned.Value = false
		print(Stunned.Value)
		Stun_Time = 0
		end
		end
	end

Damage_Remote.OnServerEvent:Connect(Damage_Target)
1 Like

you can use break
123123123123123123123

In loops, you can use the break statement to stop the loop. In order to iterate to the next item, you can use continue and you can use return to break the loop & return a value in a function.

Here’s an updated code implementation with a break statement:

function Update_Stun_Time()
    if Stun_Time >= 0 then
        repeat task.wait(0.1)
            print(Stun_Time)
            Stun_Time -= 1
        until Stun_Time <= 0

        if Stun_Time <= -0.1 then
            Stun_Time = 0

            break
        end
    end
end

And for the Damage_Target function

if Stun_Time >= 0 then
    repeat 
        task.wait(0.2)
        print(Stun_Time)
        Stun_Time -= 1
    until Stun_Time <= 0

    if Stun_Time <= 0 then
        Blood_Clone:Destroy()
        Stunned.Value = false
        print(Stunned.Value)
        Stun_Time = 0

        break
    end
end

Don’t forget to mark this post as the solution if this helped :slightly_smiling_face: