How expensive would this be?

Hey, so I’m going to be creating a booster system where players can purchase XP Booster developer products where their gained XP will be doubled for a limited amount of time.

I was thinking about ways I could go about this, and the easiest way I found was to initiate a while loop when ever a player joins in the game; and when the player leaves it’ll break out of the loop obviously. And in the loop it’d check a value within the player called like “XP_Boost_Time” and if it’s larger than 0, it’ll subtract it by 1 each second. When a player purchases a product, it’ll increment that value by the number of seconds for whatever booster they bought, and it’d save when they leave.

I’m not sure if this could be expensive if a large number of players were in the game, if it’d be easier just to create one loop that iterates over all the players in the game and runs the same condition, or so fourth.

Just looking for the best outline for this. Thanks!

I would not save the number of seconds, but instead simply save the tick() when they bought it, and check the difference of the current tick and the previous tick for checking if it’s over. You could also save a second value called hasBoost, or maybe boostMultiplier, and save what their current boost amount should be, updated once the tick() is passed the seconds they should own it. If you want to have multiple durations at different prices, you should save the amount of seconds that they should have the boost enabled as well (don’t subtract from this, just use this for your tick comparison).

You only really need to check what their XP boost is when they gain XP, but if you want to use a loop, it should not be expensive at all if you only check every 1 second. E.g. in pseudocode;

while wait(1) do
    if player.Stats.hasBoost.Value and tick() - player.Stats.Tick.Value > durationOfBoost then
        player.Stats.hasBoost.Value = false
    end
end

Edit: It will be less expensive to loop through all the Players in one while wait(1) do loop than it would be to have a while wait(1) do loop for each player, but even then it still shouldn’t be a problem if you are only checking once per second.

2 Likes

It also depends on how many players per server. 20 while loops isnt bad compared to 100, or even 200.

Maybe your players have some sort of while loop running already and you could place it within those while loops, to save yourself some processing.

If those while loops run wait() you could use a variable called updateboosttime and when it reaches 33.333 (1/0.03) approximately one second has passed. Although that depends on the framerates. Maybe not the best solution for what your accomplishing, unless you dont care if your players get a little bonus time :wink:

Also, Hi Hazania :stuck_out_tongue:

1 Like