Help optimizing script

  • Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

  • LOOKING FOR A VERSION OF SKRIPT?

    You can always check out skUnity Downloads for downloads and any other information about Skript!

banduriku

New Member
Apr 15, 2024
5
0
1
26
Hello! I'm still new to script and learning as I go. After some trial and error i found out that chaining if statements is kinda unreliable, so I figured out how to use loop instead, but I worry it will run my script excessively and cause lag when the server is full of players. Please help me figure out how to optimize this code so it will only run when the player is wearing a particular item (the loop is to ensure the item breaks because for some reason if I use script to reduce durability it won't actually break the item.)

Code:
on player step:
    lore of player's boots contains "&eto gain experience while you walk!":
        give 1 xp to the player
        
on player level up:
    lore of player's boots contains "&eto gain experience while you walk!":
        damage boots of player by 1
    
on player step:       
    loop items in player's inventory:
        if durability of player's boots is 0:
            delete player's boots
            play sound "entity.item.break" with volume 0.8
 
1) chaining if statements is not unreliable, you probably did something wrong
2) as the description of the step event says, it may cause lag on high player counts

I don't know if there is a good way to do what you are doing, I would probably have an `every 30 seconds:` event which calculates how many cm the player walked since 30 seconds ago by getting their statistic value (you can get statistic values with SkBee)
 
I can add here. You can use on armor change and then a while loop to compare the player's old location to a new location (and then update the old location to current location) and add xp that way, which may be more efficient.

Also I don't think the last thing is necessary. When the durability reaches 0, it'll automatically break and play the sound.
Furthermore...what was the point of looping the items in their inventory in that last section if you're just checking their boots?
 
I can add here. You can use on armor change and then a while loop to compare the player's old location to a new location (and then update the old location to current location) and add xp that way, which may be more efficient.

Also I don't think the last thing is necessary. When the durability reaches 0, it'll automatically break and play the sound.
Furthermore...what was the point of looping the items in their inventory in that last section if you're just checking their boots?
I've been running into a strange bug in which the armor won't actually break, it will just infinitely sit at zero. That's why I implemented this failsafe. I've also been running into issues with syntax when it comes to looping inventory, is there a way to run the loop for just the boots slot?