Solved how to swap inventory item to hand?

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

Status
Not open for further replies.

mikeab7925

New Member
Dec 19, 2017
8
0
1
I've tried many different ways to achieve this and seem to not be able to get anything to work. What I'm trying to do, is replace a broken tool from the players inventory into the active tools slot or main hand item. Where I keep getting stuck is moving the item to the active tool bar slot. What I have so far that works is:

Closest I've got was: (didn't cause errors, but doesn't work)
code_language.skript:
on tool breaking:
#   message "tool broke"
   set {_tool} to event-item
#   message "tool is %{_tool}%"
   wait 2 ticks
   if player's tool is empty:
#       message "tool slot empty"
       loop items in player's inventory:
#           message "%loop-item%"
           if loop-item is {_tool}:
               message "found tool"
               set loop-item to tool

How do I get this item to swap to the active slot, main hand?

Sorry if this is simple, I've can't seem to get this to work and have most likely searched with the wrong terms for help.
[doublepost=1524463724,1524454097][/doublepost]It seems that the variable {_tool} is being changed from "wooden_pickaxe" (my testing tool) to "0 air". Is there a reason why this is happening, and is there anything I can do to fix this?
code_language.skript:
on tool breaking:
    message "%event-item% broke"
# message is "wooden_pickaxe broke"
    set {_tool} to event-item
    message "{_tool} is %{_tool}%"
# message is "{_tool} is wooden_pickaxe"
    wait 2 ticks
    if player's tool is empty:
        message "tool slot empty"
# message is "tool slot empty"
        message "%{_tool}% now is {_tool}"
# message is "0 air now is {_tool}"
        message "event-item now is %event-item%"
# message is "event_item now is 0 air"
        loop items in player's inventory:
            message "loop-item : %loop-item%"
# message is "loop-item : wooden_pickaxe"
            if loop-item is {_tool}:
                message "found tool"
# no message
                set loop-item to tool
Solved:
Finished Code -
code_language.skript:
on tool break:
    set {_tool} to id of event-item
    wait 1 tick
    if player's tool is empty:
        loop items in player's inventory:
            if loop-item's id is {_tool}:
                set tool to the loop-item
                clear the loop-item
                exit the loop
 
Last edited:
on tool breaking:
set {_tool} to "%event-item%" parsed as an item
set {_slot} to player's current hotbar slot
wait 1 tick
loop all items in player's inventory:
if "%loop-item%" = "%{_tool}%":
remove loop-item from player's inventory
set slot {_slot} of player's inventory to {_tool}
stop
 
I should have mentioned I'm only using the latest version of Skript.

With your code I get:
Can't understand this expression: 'player's current hotbar slot' (new.sk, line 7: set {_slot} to player's current hotbar slot')

I'm trying to get the item to move to the active slot and not remove and replace. That would make a possibly damaged item be made new, wouldn't it?
[doublepost=1524468404,1524467828][/doublepost]Changing my code from "set {_tool} to event-item" to "set {_tool} to "%event-item%" parsed as an item" got it a bit further though.

code_language.skript:
on tool break:
    message "%event-item% broke"
# message is "wooden_pickaxe broke"
    set {_tool} to "%event-item%" parsed as an item
    message "{_tool} is %{_tool}%"
# message is "{_tool} is wooden_pickaxe"
    wait 2 ticks
    if player's tool is empty:
        message "tool slot empty"
# message is "tool slot empty"
        message "%{_tool}% now is {_tool}"
# message is "0 air now is {_tool}"
        message "event-item now is %event-item%"
# message is "event_item now is 0 air"
        loop items in player's inventory:
            message "loop-item : %loop-item%"
# message is "loop-item : wooden_pickaxe"
            if loop-item is {_tool}:
                message "found tool"
# message is "found tool"
                set loop-item to tool

It now found the tool in the inventory during the loop. Now if I can just get the slot of the item in the inventory to change slots to the active tools slot.
THANKS for helping me get a step further.
 
J
I should have mentioned I'm only using the latest version of Skript.

With your code I get:
Can't understand this expression: 'player's current hotbar slot' (new.sk, line 7: set {_slot} to player's current hotbar slot')

I'm trying to get the item to move to the active slot and not remove and replace. That would make a possibly damaged item be made new, wouldn't it?
[doublepost=1524468404,1524467828][/doublepost]Changing my code from "set {_tool} to event-item" to "set {_tool} to "%event-item%" parsed as an item" got it a bit further though.

code_language.skript:
on tool break:
    message "%event-item% broke"
# message is "wooden_pickaxe broke"
    set {_tool} to "%event-item%" parsed as an item
    message "{_tool} is %{_tool}%"
# message is "{_tool} is wooden_pickaxe"
    wait 2 ticks
    if player's tool is empty:
        message "tool slot empty"
# message is "tool slot empty"
        message "%{_tool}% now is {_tool}"
# message is "0 air now is {_tool}"
        message "event-item now is %event-item%"
# message is "event_item now is 0 air"
        loop items in player's inventory:
            message "loop-item : %loop-item%"
# message is "loop-item : wooden_pickaxe"
            if loop-item is {_tool}:
                message "found tool"
# message is "found tool"
                set loop-item to tool

It now found the tool in the inventory during the loop. Now if I can just get the slot of the item in the inventory to change slots to the active tools slot.
THANKS for helping me get a step further.
Just copy my code
 
Just copying your code give me the "Can't understand this expression: 'player's current hotbar slot' (new.sk, line 7: set {_slot} to player's current hotbar slot')" error.

So far I have it mostly working. I just need to get it to remove the old item.
code_language.skript:
on tool break:
    set {_tool} to id of event-item
    wait 2 ticks
    if player's tool is empty:
        loop items in player's inventory:
            if loop-item's id is {_tool}:
                set tool to the loop-item
 
code_language.skript:
on tool breaking:
    set {_tool} to "%event-item%" parsed as an item
    set {_slot} to player's current hotbar slot
    send "%{_tool}% broke"
    wait 1 tick
    loop all items in player's inventory:
        if "%loop-item%" = "%{_tool}%":
            remove loop-item from player's inventory
            set slot {_slot} of player's inventory to {_tool}
            stop
[doublepost=1524471175,1524471144][/doublepost]
Just copying your code give me the "Can't understand this expression: 'player's current hotbar slot' (new.sk, line 7: set {_slot} to player's current hotbar slot')" error.

So far I have it mostly working. I just need to get it to remove the old item.
code_language.skript:
on tool break:
    set {_tool} to id of event-item
    wait 2 ticks
    if player's tool is empty:
        loop items in player's inventory:
            if loop-item's id is {_tool}:
                set tool to the loop-item
You need to have skquerry
[doublepost=1524471260][/doublepost]
code_language.skript:
on tool break:
    set {_tool} to id of event-item
    wait 2 ticks
    if player's tool is empty:
        loop items in player's inventory:
            if loop-item's id is {_tool}:
                remove loop-item from player's inventory
                set tool to the loop-item
 
I've already tried adding in the "remove loop-item from player's inventory" before the "set tool to the loop-item". It removes the item before it's copied. When I do it after, It removes the item from the active slot.
 
I've already tried adding in the "remove loop-item from player's inventory" before the "set tool to the loop-item". It removes the item before it's copied. When I do it after, It removes the item from the active slot.
It works fine for me, It needs to be removed before its placed in the tool slot? Or does it not replace the player's tool?
 
If the code is before the "set tool to the loop-item", it's removed before it replaces it. If it's after it replaces it, then removes the replacement and leaves the original. It might be different for you due to the skquerry.
 
Try this
code_language.skript:
on tool break:
    set {_tool} to id of event-item
    wait 2 ticks
    if player's tool is empty:
        loop items in player's inventory:
            if loop-item's id is {_tool}:
                set {_item} to loop-item
                remove loop-item from player's inventory
                set player's tool to {_item}
 
  • Like
Reactions: mikeab7925
I got it to work
code_language.skript:
on tool break:
    set {_tool} to id of event-item
    wait 1 tick
    if player's tool is empty:
        loop items in player's inventory:
            if loop-item's id is {_tool}:
                set tool to the loop-item
                clear the loop-item
                exit the loop
Thanks for the replies.
 
Status
Not open for further replies.