Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can too many loops lag my game at all?

Asked by 4 years ago

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.

1
it can depending on what they do sean_thecoolman 189 — 4y
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? NoahsRebels 99 — 4y
1
its pretty small so it probably wouldnt lag, but something like 10 times bigger than this or whatever will probably lag sean_thecoolman 189 — 4y
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. greatneil80 2647 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

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)
Ad

Answer this question