Should i use a while loop or a repeat loop or a for i,v loop in this case?

Hello, in my game I have lots of situations in which I can choose to use a while loop or a repeat loop (and in some case a for i,v loop). But I don’t know which one is better, especially for performance (many loops are in the server).

Here is an exemple of for i,v loop, then i’ll show an exemple of repeat loop for the same tasks

rs.ClientEvents.MobileGearsTrop.OnServerEvent:Connect(function(plr)
	if #plr.Tout.Gears:GetChildren() <= 7 then plr.Tout.ExploitCheck.Value += 1 if plr.Tout.ExploitCheck.Value >5 then plr:Kick('Stop Exploit please') end return end
	for i,v in plr.Tout.Gears:GetChildren() do
		v:Destroy()
		if #plr.Tout.Gears:GetChildren() <= 7 then
			break
		end
		task.wait()
	end
end)

So in this for i,v loop i detect the amount of children , if the amount is over 7 then i destroy some children untl the amount of children is <=7

Now let’s see the same script with a repeat

rs.ClientEvents.MobileGearsTrop.OnServerEvent:Connect(function(plr)
	if #plr.Tout.Gears:GetChildren() <= 7 then plr.Tout.ExploitCheck.Value += 1 if plr.Tout.ExploitCheck.Value >5 then plr:Kick('Stop Exploit please') end return end
	repeat
		plr.Tout.Gears:FindFirstChildOfClass('BoolValue'):Destroy()
		task.wait()
	until #plr.Tout.Gears:GetChildren() <= 7 
end)

i could do the same with a while loop but you understand what i mean. Could you tell me which one should i use and Overall which one is the better for performance ? Any help advice is welcolme and apprecied ! Thanks

1 Like

Now obviously some are better then others performance wise however it’s not really why there are multiple loops. Loops normally have different purposes such as a for loop just loops through numbers or somewhere in the explorer. Repeat loops will always run at least once no matter what because it checks the condition at the end of the code whereas a while loop always checks the condition before hand. So it basically just depends on the situation.

2 Likes

yes but for most of the loop i can handle with a repeat or a while loop , one check the condition before the other one check the condition after, which don’t represent a big difference, but i don’t know which one would be best for performance

They don’t really effect performance that much unless there is no task.wait() to slow it down. You shouldn’t have to worry about which one to use.

Alright thanks for help ! char

1 Like

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