• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua How to optimize this script

ZeroSkyer

Member
Joined
May 17, 2019
Messages
62
Reaction score
9
Location
EEUU
Hello friends, I am trying to lower the level of the players in certain events. I have achieved it and with storage I store its previous level and return it to you when you leave the event or at the end of it. The problem I face is that when there are a considerable amount of players the script causes a few seconds of freeze. Could someone help me to optimize? Part of the code.

Lua:
function getExpForLevel(level)
    level = level - 1
    return ((50 * level * level * level) - (150 * level * level) + (400 * level)) / 3
end

To remove the level
Lua:
local realLevel = player:getLevel()
if player:getStorageValue(PrivateWar.levelReduced) < 1 then
    player:setStorageValue(PrivateWar.levelLimit, realLevel)
    player:setStorageValue(PrivateWar.levelReduced, 1)
    player:removeExperience(player:getExperience() - getExpForLevel(300), false)
end

To re-add the level
Lua:
local realLevel = player:getStorageValue(PrivateWar.levelLimit)
if player:getStorageValue(PrivateWar.levelReduced) == 1 then
    player:setStorageValue(PrivateWar.levelReduced, 0)
    player:addExperience(getExpForLevel(realLevel) - player:getExperience(), false)
end
 
There is basically nothing to optimize here.
You could get rid of the getExpForLevel(300) and input the calculation directly.. 441,084,800

But most of your issue is from a considerable amount of players being inside the event.
The only thing I can suggest is to spread out the work-load of removing the players from the event.

Some quick examples of ways to spread out the workload;
1) Have them exit via a teleporter. (Players generally understand what exit teleports are, so is a very easy insertion)
2) Create a function that removes X amount of players from the event. Loop the function with a 1 second delay until all players have been removed. (It's automatic, spreads out the load, and players won't really notice the difference between a 1 second wait, or a 10 second wait period to exit.)
 
Thank you, I will follow your advice to distribute the load. Regarding the function of adding exp, is there a way to optimize it?
 
Thank you, I will follow your advice to distribute the load. Regarding the function of adding exp, is there a way to optimize it?
Only way I can think of is to change the function to store the exact amount of experience the player is losing.

(using small numbers for example's sake)

level 300 = 1000 experience (you can look-up this number and manually input it)
player's current experience is 2500. (don't need to know the player's level.)

2500 - 1000 = 1500 experience -> store this number in a storage.

Then you can remove 1500 experience or add 1500 experience, by accessing the storage value.

--
In this way, there is no multiplying of numbers, just addition and subtraction.
 
Back
Top