Need help refining my Skript for gens and PvP combo

  • 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!

knirtcelE

Member
May 29, 2023
1
0
1
23
Hello everyone,

I'm relatively new to Skript and I've been working on a gens and PvP combo script for my server. However, I've been experiencing significant lag issues despite hosting the server locally on a powerful computer with 32GB of RAM and an i7-9700K processor. I would appreciate any advice or suggestions to optimize the performance of my script.

Here's the current version of my Skript code:





Code:
on load:
    set {gens::one} to light blue terracotta named "&d&lGen I" with lore "&8----------" and "&7Produces one gem every 5 seconds"
    set {gens::two} to  blue concrete named "&d&lGen II" with lore "&8----------" and "&7Produces two gems every 5 seconds"
    set {gens::three} to purple concrete named "&d&lGen III" with lore "&8----------" and "&7Produces three gems every 5 seconds"
    set {gens::four} to  magenta concrete named "&d&lGen IV" with lore "&8----------" and "&7Produces four gems every 5 seconds"
    set {items::gem} to amethyst shard named "&aGem" with lore "&8----------" and "&7Sell for items at a shop"
    set {items::compressedgem} to block of amethyst named "&a&lCompressed Gem" with lore "&8----------" and "&7Sell for items at a shop"
    set {items::shop} to crafting table named "&2Shop" with lore "&8----------" and "&7Place to use"

command /givegen [<text>]:
    permission: op
    trigger:
        if arg-1 is set:
            give player {gens::%arg-1%}
        else:
            message "&cInvalid Arguments! Usage: /givegen <gen>"

command /giveitem [<text>]:
    permission: op
    trigger:
        if arg-1 is set:
            give player {items::%arg-1%}
        else:
            message "&cInvalid Arguments! Usage: /givegen <gen>"

on place:
    if player's tool is light blue terracotta:
        placeGen("one", player, event-block's location)
    if player's tool is blue concrete:
        placeGen("two", player, event-block's location)


on place:
    if player's tool is purple concrete:
        placeGen("three", player, event-block's location)
    if player's tool is magenta concrete:
        placeGen("four", player, event-block's location)



on rightclick on a crafting table:
    cancel event
    wait 1 tick
    make console execute command "/shopkeeper open Shop %player%"

on block break:
    if event-block is not light blue terracotta or blue concrete or magenta concrete or purple concrete or oak planks or crafting table:
        cancel event


on block break:
    if event-block is light blue terracotta:
        cancel event
        set event-block to air
        breakGen("one", player, event-block's location)
        drop {gens::one}
    if event-block is blue concrete:
        cancel event
        set event-block to air
        breakGen("two", player, event-block's location)
        drop {gens::two}


on block break:
    if event-block is purple concrete:
        cancel event
        set event-block to air
        breakGen("three", player, event-block's location)
        drop {gens::three}
    if event-block is magenta concrete:
        cancel event
        set event-block to air
        breakGen("four", player, event-block's location)
        drop {gens::four}

        

function placeGen(n: text, p: player, l: location):
    add {_l}'s location to {gensplaced::%{_n}%::%{_p}'s uuid%::*}
    play sound "block.note_block.pling" at volume 1 at pitch 2 to {_p}





function breakGen(n: text, p: player, l: location):
    remove {_l}'s location from {gensplaced::%{_n}%::%{_p}'s uuid%::*}
    play sound "block.note_block.pling" at volume 1 at pitch 1 to {_p}

on join:
    give player wooden sword
    give player 32 oak planks
    give player {gens::one}
    give player leather chestplate
    give player {items::shop}
    score(player)
    if {kills.%player's uuid%.kills_total} is not set:
        set {kills.%player's uuid%.kills_total} to 0







every 5 seconds:
    loop all players:
        loop all blocks in radius 30 around loop-player:
            if {gensplaced::one::%loop-player's uuid%::*} contains loop-block's location:
                set {_loc} to block above loop-value-2
                drop 1 of {items::gem} at {_loc} without velocity
                make 5 of happy villager at {_loc} with offset vector(0.5, 0.5, 0.5)
            if {gensplaced::two::%loop-player's uuid%::*} contains loop-block's location:
                set {_loc} to block above loop-value-2
                drop 2 of {items::gem} at {_loc} without velocity
                make 5 of happy villager at {_loc} with offset vector(0.5, 0.5, 0.5)
            if {gensplaced::three::%loop-player's uuid%::*} contains loop-block's location:
                set {_loc} to block above loop-value-2
                drop 3 of {items::gem} at {_loc} without velocity
                make 5 of happy villager at {_loc} with offset vector(0.5, 0.5, 0.5)
            if {gensplaced::four::%loop-player's uuid%::*} contains loop-block's location:
                set {_loc} to block above loop-value-2
                drop 4 of {items::gem} at {_loc} without velocity
                make 5 of happy villager at {_loc} with offset vector(0.5, 0.5, 0.5)



function resetInventorys():
    loop all players:
        clear loop-player's inventory
        give loop-player wooden sword
        give loop-player 32 oak planks
        give loop-player {gens::one}
        give loop-player leather chestplate
        give loop-player {items::shop}


function gameStartFlags():
    execute console command "/rg flag arena build allow"
    execute console command "/rg flag arena pvp deny"

function delGens():
    loop all players:
        delete {gensplaced::one::%loop-player's uuid%::*}
        delete {gensplaced::two::%loop-player's uuid%::*}
        delete {gensplaced::three::%loop-player's uuid%::*}
        delete {gensplaced::four::%loop-player's uuid%::*}
        loop blocks in radius 50 around loop-players:
            if loop-block is light blue terracotta or blue concrete or purple concrete or magenta concrete or oak planks or crafting table:
                set {_loc} to loop-block's location
                set block at {_loc} to air

command /testsound:
    trigger:
        play sound "entity.ender_dragon.growl" with volume 1 to all players


command /startgame <integer>:
    trigger:
        if {start} is not true:
            set {start} to true
            set {_countdown} to 100/arg 1
            resetInventorys()
            delGens()
            loop all players:
                set {kills.%loop-player's uuid%.kills_total} to 0
            score(player)
            execute console command "rg flag arena build -w world allow"
            execute console command "rg flag arena pvp -w world deny"
            play sound "block.note_block.pling" at volume 1 at pitch 1
            send title "&d&lBattle Started!" with subtitle "&aYou have 20 seconds of invincibility!" to player for 3 seconds
            message "&aThe game has started."
            create bossbar titled "&a&lYou are invincible!" and id "invincible" for all players with progress 100 with colors green
            set {i} to 5
            loop 20 times:
                set bossbar "invincible" value to {i}
                add 5 to {i}
                wait 1 second
            remove bossbar "invincible"
            delete {i}
            execute console command "rg flag arena pvp -w world allow"
            play sound "entity.ender_dragon.growl" with volume 1 to all players
            send title "&c&lPvP is now enabled" with subtitle "" to all players for 3 seconds
            create bossbar titled "&7&lTime Remaining" and id "time" for all players with progress 0 with colors blue
            set {time} to {_countdown}
            loop arg 1 times:
                set bossbar "time" value to {time}
                add {_countdown} to {time}
                wait 1 second
            remove bossbar "time"
            delete {time}
            delete {_countdown}
            if {topkiller} is "":
                create bossbar titled "&4&lDeathmatch" and id "deathmatch" for all players with progress 100 with colors red
                send title "&4&lDeathmatch" with subtitle "&cFirst kill wins!" to all players for 3 seconds
                while {topkiller} is "":
                    wait 1 tick
                delGens()
                execute console command "rg flag arena build -w world deny"
                execute console command "rg flag arena pvp -w world deny"
                loop all players:
                    set {kills.%loop-player's uuid%.kills_total} to 0
                remove bossbar "deathmatch"
                send title "&d&l%{topKiller}% &6wins!" with subtitle "%{topKiller}% won with %{topkills}% kills!" to all players for 3 seconds
                set {topKiller} to ""
                set {topKills} to 0
                set {start} to false
                score(player)
            else:
                delGens()
                execute console command "rg flag arena build -w world deny"
                execute console command "rg flag arena pvp -w world deny"
                loop all players:
                    set {kills.%loop-player's uuid%.kills_total} to 0
                remove bossbar "time"
                send title "&d&l%{topKiller}% &6wins!" with subtitle "%{topKiller}% won with %{topkills}% kills!" to all players for 3 seconds
                set {topKiller} to ""
                set {topKills} to 0
                set {start} to false
                score(player)
        else:
            send "&cGame already active." to Player
        

command /forceend:
    trigger:
        set {start} to false
        delGens()
        execute console command "rg flag arena build -w world deny"
        execute console command "rg flag arena pvp -w world deny"
        loop all players:
            set {kills.%loop-player's uuid%.kills_total} to 0
            remove bossbar "time"
            remove bossbar "deathmatch"
            remove bossbar "invincible"
            score(player)
            loop blocks in radius 50 around loop-players:
                if loop-block is light blue terracotta or blue concrete or purple concrete or magenta concrete or oak planks or crafting table:
                    set {_loc} to loop-block's location
                    set block at {_loc} to air
        send title "&7&lGame Ended." with subtitle "&8-------" to all players for 3 seconds
        set {topKiller} to ""
        set {topKills} to 0


command /winning:
    trigger:
        send "Top Kills:%{topkills}%" to player
        send "Top Killer:%{topkiller}%" to player
        send "Kills:%{kills.%player's uuid%.kills_total}%" to player






on load:
    set {topKiller} to ""
    set {topKills} to 0




on death:
    attacker is a player
    add 1 to {kills.%attacker's uuid%.kills_total}
    if {kills.%attacker's uuid%.kills_total} is greater than {topKills}:
        set {topKiller} to attacker's display name
        set {topKills} to {kills.%attacker's uuid%.kills_total}
    send "&a+1 Kill" to attacker
    loop all players:
        score(loop-player)


on disconnect:
    clear the player's inventory
    delete {gensplaced::one::%player's uuid%::*}
    delete {gensplaced::two::%player's uuid%::*}
    delete {gensplaced::three::%player's uuid%::*}
    delete {gensplaced::four::%player's uuid%::*}
    delete {kills.%player's uuid%.kills_total}
    loop all players:
        if {kills.%loop-player's uuid%.kills_total} is greater than {topKills}:
            set {topKiller} to loop-player's display name
            set {topKills} to {kills.%loop-player's uuid%.kills_total}




command /resetkills:
    trigger:
        loop all players:
            delete {kills.%loop-player's uuid%.kills_total}
        set {topKiller} to ""
        set {topKills} to 0
        send "&cAll player kills have been reset." to player


function score(p: player):
    set {_uuid} to {_p}'s uuid
    set {_topKiller} to {topKiller}
    set {_topKills} to {topKills}
    set title of {_p}'s scoreboard to "&8---&d&lGem Battles&8---"
    if {start} is true:
        set line 3 of {_p}'s scoreboard to "&dGame: &a&lActive"
    else:
        set line 3 of {_p}'s scoreboard to "&dGame: &c&lInactive"
    set line 2 of {_p}'s scoreboard to "&dKills: &7%{kills.%{_p}%.kills_total}%"
    set line 1 of {_p}'s scoreboard to "&dTop Kills:"
    if {_topKiller} is "":
        set line 0 of {_p}'s scoreboard to "&7N/A"
    else:
        set line 0 of {_p}'s scoreboard to "&7%{_topKiller}%&7: %{_topKills}% Kills"
    toggle {_p}'s scoreboard on


I believe the script should be working, but I'm experiencing lag on my server. I've already confirmed that FPS lag and ping lag are not the issue. My computer specifications should be more than sufficient to handle the load.

If anyone has any ideas or optimizations to improve the performance of this script, I would greatly appreciate it. I'm open to any suggestions to make it run more smoothly and efficiently.

Thank you in advance for your help!

Skript Ver: 2.6.4
Paper: 1.19.4-540
 
loop all blocks in radius ** - is expensive operation. Try adding a delay of 1 tick every N iterations like
Code:
loop ...block in radius N around player... times:
  add 1 to {_i}
  if mod({_i},100) is 0:
    wait 1 tick
[doublepost=1685400479,1685400303][/doublepost]Or if your task is to remove blocks placed by players - add the location of each placed block to the list, and when you need to remove them - iterate through this list and `set block at %looped-location% to air`.
 
Last edited: