Solved Optimizing sell 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 community!

    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!

BruhedPeanut

Member
Feb 15, 2024
3
0
1
Hello, I am a relatively new skripter and would like to optimize my selling script for a mining simulator.

Code:
command /sell:
    trigger:
        loop 2304 times:
            loop all items in the player's inventory:
                if loop-item is coal:
                    remove 1 coal from player's inventory
                    add 250 to {amountgive::%player%}
                    add 250 to {balance::%player%}
                if loop-item is cobblestone:
                    remove 1 cobblestone from player's inventory
                    add 10 to {amountgive::%player%}
                    add 10 to {balance::%player%}
                if loop-item is raw gold:
                    remove 1 raw gold from player's inventory
                    add 1000 to {amountgive::%player%}
                    add 1000 to {balance::%player%}
                if loop-item is diamond:
                    remove 1 diamond from player's inventory
                    add 2000 to {amountgive::%player%}
                    add 2000 to {balance::%player%}
        message "&7[&6PeanutSim&7]&6 Sold items for $%{amountgive::%player%}%" to player
        subtract {amountgive::%player%} from {amountgive::%player%}

The current code is fine but the game lags due to the script looping 2304 times. I've tried to detect all at once but failed. Is there a way to achieve this? The on ly addon to skript I have is SkBee. Let me know if this is only possible with a certain addon, I will gladly add it and fix it. Thanks!
 
Woah, this code is extremely unoptimized, also dont use list values and %player%, use %player's uuid% because if the player changes their name, progress is gone.
Try this instead.

Code:
command /sell:
    trigger:
        set {_prev} to {balance::%player%}
        add amount of cobblestone in player's inventory * 10 to {balance::%player%}
        add amount of raw gold in player's inventory * 1000 to {balance::%player%}
        add amount of coal in player's inventory * 250 to {balance::%player%}
        add amount of diamond in player's inventory * 2000 to {balance::%player%}
        remove all cobblestone, raw gold, coal and diamond from player's inventory
        send "&7[&6PeanutSim&7]&6 Sold items for $%{balance::%player%} - {_prev}%"
 
No problem, but i do recommend going through and changing everything to balance::%player's uuid% in your files though