Tutorial by: Aabsss


In this tutorial, I am going to be showing you how to make an auto compressor.

In this example, I am gonna use a wood compressor

First you would want to make a simple function with a player parameter

function autoCompress(player: player):

Next, you would want to see if that player has the compressor in their inventory or, in their hand, but for this example I'm going to choose in their inventory

function autoCompress(player: player):
    if {_player}'s inventory contains oak wood named "&aAuto Wood Compressor" with lore "&7Automatically compresses oak logs!":

Next, you are gonna want to see if they have the amount of blocks you want for compressing, I'm doing thirty-two

function autoCompress(player: player):
    if {_player}'s inventory contains oak wood named "&aAuto Wood Compressor" with lore "&7Automatically compresses oak logs!":
        if {_player} has 32 oak logs:

Now, you are gonna need to do some math:

  1. Get the number of specific blocks they have in their inventory.
  2. Divide it by how much blocks it takes for it to compress
  3. Floor it to the nearest number.
function autoCompress(player: player):
    if {_player}'s inventory contains oak wood named "&aAuto Wood Compressor" with lore "&7Automatically compresses oak logs!":
        if {_player} has 32 oak logs:
            set {_amount} to number of oak logs in {_player}'s inventory
            set {_amount} to {_amount} / 32
            set {_amount} to floor({_amount})

Great! Now we have the amount of compressed blocks they WOULD have. But they do not have them yet, so lets add it:

  1. Loop the amount of times.
  2. Remove the the amount of potential compressed blocks and the amount of uncompressed blocks it takes to compress 1 block
  3. Give the player the amount of the certain compressed block
function autoCompress(player: player):
    if {_player}'s inventory contains oak wood named "&aAuto Wood Compressor" with lore "&7Automatically compresses oak logs!":
        if {_player} has 32 oak logs:
            set {_amount} to number of oak logs in {_player}'s inventory
            set {_amount} to {_amount} / 32
            set {_amount} to floor({_amount})
            loop {_amount} times:
                remove ({_amount} * 32) of oak logs from {_player}'s inventory
                give {_amount} of oak wood named "&aCompressed Oak Log" to {_player}

Nice! Now you're done with the function. But now you have to call it, usually you can do on pickup or loop all the players every minute or so.

function autoCompress(player: player):
    if {_player}'s inventory contains oak wood named "&aAuto Wood Compressor" with lore "&7Automatically compresses oak logs!":
        if {_player} has 32 oak logs:
            set {_amount} to number of oak logs in {_player}'s inventory
            set {_amount} to {_amount} / 32
            set {_amount} to floor({_amount})
            loop {_amount} times:
                remove ({_amount} * 32) of oak logs from {_player}'s inventory
                give {_amount} of oak wood named "&aCompressed Oak Log" to {_player}

on load:
    loop all player:
        autoCompress(loop-player)

If you did it all right, it should look something like this:

function autoCompress(player: player):
    if {_player}'s inventory contains oak wood named "&aAuto Wood Compressor" with lore "&7Automatically compresses oak logs!":
        if {_player} has 32 oak logs:
            set {_amount} to number of oak logs in {_player}'s inventory
            set {_amount} to {_amount} / 32
            set {_amount} to floor({_amount})
            loop {_amount} times:
                remove ({_amount} * 32) of oak logs from {_player}'s inventory
                give {_amount} of oak wood named "&aCompressed Oak Log" to {_player}

every 10 minutes:
    loop all players:
        autoCompress(loop-player)

on pickup:
    autoCompress(player)

Did you find Aabsss's tutorial helpful?


You must be logged in to comment

  • May 20, 2024, 7:06 a.m. - Fusezion  

    Code is not tested, method for giving/removing the amounts is using a loop x times which is causing it to grant x times as much

    if a user has 96 oak logs they're suppose to get 3 compress but increase get 9

    |     
  • May 28, 2024, 9:36 p.m. - itsdakirby69  

    No need for the loop x times line.
    Otherwise not bad introduction for compressing blocks.

    |