A while loop or a repeat until for this type of scenario?

I have a little script that handles a spider-web like function that damages player every 1 second and usually has an if statements to break the loop, it works great but i’m just wondering if it’s better off to use a repeat until or a even better a greater approach?

workspace:FindFirstChild("spider web").Touched:Connect(function(__PLAYER)
	local __CHARACTERPLAYER = Players:GetPlayerFromCharacter(__PLAYER.Parent)
	if __CHARACTERPLAYER ~= nil then
		local __CHARACTER = __CHARACTERPLAYER.Character
			local __HIM = __CHARACTER:FindFirstChildOfClass("Humanoid")
			local __OCCUPIED = workspace:FindFirstChild("spider web"):FindFirstChild("__OCCUPIED").Value -- Boolean to check if somebody inside the spider web
			local __ESCAPED = ReplicatedStorage:FindFirstChild(__CHARACTERPLAYER.Name):WaitForChild("__ESCAPED").Value  -- Boolean to check if player recently escaped.
			local __ESCAPEDTIME = ReplicatedStorage:FindFirstChild(__CHARACTERPLAYER.Name):WaitForChild("__ESCAPEDTIME") -- NumberValue that goes from 20 to 0 to make the player escape
			if __OCCUPIED == false then
				__ESCAPEDTIME.Value = 20 -- force value to be 20 incase a player ever gets stuck again.
				__OCCUPIED = true
				while true do
					if __OCCUPIED == true then
					if __ESCAPED == false then
						task.wait(1) 
						__CHARACTER.HumanoidRootPart.Anchored = true -- Replicate stunning
						__HIM:TakeDamage(10) -- Damaging player every 1 second
						if __ESCAPEDTIME.Value <= 0 then -- Player escaped inside spider web
                            --__ESCAPETIME value is changed through a server invoke.
							workspace:FindFirstChild("spider web"):Destroy()
							__ESCAPED = true
							__CHARACTER.HumanoidRootPart.Anchored = false
							local BodyVelocity = Instance.new("BodyVelocity", __CHARACTER.Torso)
							ReplicatedStorage:FindFirstChild(__CHARACTERPLAYER.Name):WaitForChild("__EVENTS"):WaitForChild("__RAGDOLL"):Fire(1.2)
							BodyVelocity.Velocity = Vector3.new(0,10,0) * 15
							game.Debris:AddItem(BodyVelocity, 1)
							break -- Did they escape? Nope! continue damaging
						end
						if __HIM.Health <= 10 then -- Player has died inside spider web
								__HIM.Health = 100
							workspace:FindFirstChild("spider web"):Destroy()
							__ESCAPED = true
							__ESCAPEDTIME.Value = 20
							__CHARACTER.HumanoidRootPart.Anchored = false
							__OCCUPIED = false 
							break
						else -- did they die yet?
							continue -- nope! continue looping
						end
					end
				end
			end
		end
	end
end)

The only difference between while loops and repeat loops is that repeat loops are guaranteed to run at least once. That’s the only difference. Since you’re using while true however, there’s no difference between the two.

1 Like

First you must understand the reason of using each loop. A while loop gonna repeat the code while a condition is true and thats not exactly what you want. A repeat loop gonna repeat the code until a condition is met, so you gonna repeat the code until the character dies for example. Both loops while and repeat doesnt have a big difference, its more a matter of semantic.

1 Like

I would choose for repeat because you dont need to make a conditional structure to check if the condition is met, repeat already do it for you so… repeat loop is my advice, you must code less using it.

That’s the same as what while loops do though, it just checks in a different order and the condition is reversed. In his case however, he has some specific code related to the condition being met or I would have recommended against while true in favor of while (not) x or repeat until (not) x

Some languages have a basic loop print("Hi!") end sort of structure, but not Lua. However, to my knowledge Roblox has optimized while true do ... end to function the same as loop ... end. That’s a good thing. anyways, he’s using the right tool for the job IMO.

2 Likes

I’ll try using a repeat however is it possible to break the loop if the number value reaches 0? I know the until can be used to check if player reached <= 20 health.

Sure, the break keyword stops any loop