Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Can too many loops lag my game at all?

This may sound nooby and whatnot, but I was making a badge giver in my game that awards you a badge when the team you're on has a certain amount of Moral. I was going to make it look something like this:

while true do
	wait(.1)
	Player = game.Players:GetPlayerFromCharacter(script.Parent)
	Badge = game:GetService("BadgeService")
	if Player.Team == game.Teams.Prussians then
		
		if game.Teams.Prussians.Moral.Value >= 50 then
			Badge:AwardBadge(Player.UserId, 11223344)
		end
	end
end

This is fine, but I have quite a few other scripts that also loop quickly to find something, and I feel like a lot of loops could slow down my game (as the title implies) I could instead use a .Changed event on the value instead, though this would mean new players wouldn't get the badge until the value changes again.

4 comments

1

it can depending on what they do

0

Ah, so would something like what's above be laggy? I don't think I'll be using it because I decided the .Changed version is better for new players anyway, but if I did would it cause any lag?

1

its pretty small so it probably wouldnt lag, but something like 10 times bigger than this or whatever will probably lag

1

if you put while true do wait() print('hello') end - it will lag a lot. because its being printed every millisecond, if loops that change local variables arent that much of a lag.

1 answer

I think the answer will be very simple. use .Changed event rather than doing endless while loop.

The second issue, new users don't get the badge, it is easier to resolve, just use


game.Players.PlayerAdded:connect(function(player) 
	--give a badge 
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.