Looping a specific dropped item in a radius of X from players death point.

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

SubSplash

Member
Oct 11, 2023
17
1
3
Hello! I need help looping a specific dropped item in a radius of X from players death point and giving the looped item to the attacker also the all other items gets cleared, for example:

Code:
on death of player:
  set {playerdeath::%victim's uuid%} to location of victim
  loop all dropped items in radius 5 of location at {_playerdeath::%victim's uuid%}:
    if loop-value is iron ingot:
      set {_ironchecker.%victim's uuid%} to amount of iron ingot in loop-value
      give {_ironchecker.%victim's uuid%} of iron ingot to the attacker
 
On death gets triggered right as it happens, and the items get dropped *after* the event passes. If you want to go with this method, wait 1 tick after the death before looping the items, as the event would have passed by then. Another option would also be to loop the victim's inventory and give loop-item to the attacker, then just clear the victim's inventory as well. It'd reduce the chance of two nearby deaths causing half of victim 1's inventory being sent to attacker 2 on accident.

I also noticed on line 2 you're setting a global variable while line 3 uses a local variable. Because you haven't set the local variable referenced on line 3, it's returning <none>, and you're trying to get the dropped items next to <none>, which isn't possible. To fix it, I'd make the variable on line 2 local (by prefixing it with a _, like you did).
 
On death gets triggered right as it happens, and the items get dropped *after* the event passes. If you want to go with this method, wait 1 tick after the death before looping the items, as the event would have passed by then. Another option would also be to loop the victim's inventory and give loop-item to the attacker, then just clear the victim's inventory as well. It'd reduce the chance of two nearby deaths causing half of victim 1's inventory being sent to attacker 2 on accident.

I also noticed on line 2 you're setting a global variable while line 3 uses a local variable. Because you haven't set the local variable referenced on line 3, it's returning <none>, and you're trying to get the dropped items next to <none>, which isn't possible. To fix it, I'd make the variable on line 2 local (by prefixing it with a _, like you did).
Thank you for replying, I will try the things that you have said.
 
On death gets triggered right as it happens, and the items get dropped *after* the event passes. If you want to go with this method, wait 1 tick after the death before looping the items, as the event would have passed by then. Another option would also be to loop the victim's inventory and give loop-item to the attacker, then just clear the victim's inventory as well. It'd reduce the chance of two nearby deaths causing half of victim 1's inventory being sent to attacker 2 on accident.

I also noticed on line 2 you're setting a global variable while line 3 uses a local variable. Because you haven't set the local variable referenced on line 3, it's returning <none>, and you're trying to get the dropped items next to <none>, which isn't possible. To fix it, I'd make the variable on line 2 local (by prefixing it with a _, like you did).
Sorry for replying again but I'm have troubles getting this to work. So far I have this.

Code:
on death of player:
  if victim's inventory contains 1 iron ingot:
    set {_ironchecker} to amount of iron ingot in victim's inventory
    give {_ironchecker} of iron ingot to attacker
    clear victim's inventory
    broadcast "%{_ironchecker}%"
 
Sorry for replying again but I'm have troubles getting this to work. So far I have this.

Code:
on death of player:
  if victim's inventory contains 1 iron ingot:
    set {_ironchecker} to amount of iron ingot in victim's inventory
    give {_ironchecker} of iron ingot to attacker
    clear victim's inventory
    broadcast "%{_ironchecker}%"
"at least" 1 iron ingot. Right now, if the victim's inventory contains 1 and only 1 iron ingot, it'll clear the victim's inventory and give 1 iron ingot to the player.

Edit: I'd do "if amount of iron ingot in victim's inventory is greater than one" instead. Not sure if "at least" is a thing.
 
"at least" 1 iron ingot. Right now, if the victim's inventory contains 1 and only 1 iron ingot, it'll clear the victim's inventory and give 1 iron ingot to the player.

Edit: I'd do "if amount of iron ingot in victim's inventory is greater than one" instead. Not sure if "at least" is a thing.
I tried doing what you said but it seems to not be working. Example:

Code:
on death of player:
  broadcast "test1"
  if amount of iron ingot in victim's inventory is greater than 1:
    broadcast "test2"
    set {_ironchecker} to amount of iron ingot in victim's inventory
    give {_ironchecker} of iron ingot to attacker
    clear victim's inventory
    broadcast "test3 %{_ironchecker}%"

The only message that broadcasts is the first one.
 
Sorry, greater than or equal to one*. I'm not able to open Minecraft at the moment, but broadcast the amount of iron ingot in the victim's inventory and start debugging from there. Typically, if a condition isn't being met when it should, checking both the value and the test value (the thing the value is being compared against) is a good start. Since we know 1 isn't ambiguous, the problem is somewhere with the amount of iron ingot.

If that seems fine, I wonder if you'd be able to set a single value or a list to "all of iron ingot in victim's inventory" and see what that returns.

Or just use "if victim's inventory contains iron ingot:"
...I'm not sure why I didn't think of that originally.
 
Sorry, greater than or equal to one*. I'm not able to open Minecraft at the moment, but broadcast the amount of iron ingot in the victim's inventory and start debugging from there. Typically, if a condition isn't being met when it should, checking both the value and the test value (the thing the value is being compared against) is a good start. Since we know 1 isn't ambiguous, the problem is somewhere with the amount of iron ingot.

If that seems fine, I wonder if you'd be able to set a single value or a list to "all of iron ingot in victim's inventory" and see what that returns.

Or just use "if victim's inventory contains iron ingot:"
...I'm not sure why I didn't think of that originally.
I checked and broadcasted the amount of iron ingots in the victims inventory but it returned 0 even tho the victims inventory contains 4. Here's the code I used.

Code:
on death of player:
  set {_ironchecker} to amount of iron ingot in victim's inventory
  broadcast "%{_ironchecker}%"
 
I checked and broadcasted the amount of iron ingots in the victims inventory but it returned 0 even tho the victims inventory contains 4. Here's the code I used.

Code:
on death of player:
  set {_ironchecker} to amount of iron ingot in victim's inventory
  broadcast "%{_ironchecker}%"
Here's an idea (and again, I'm seriously sorry that I can't test stuff right now. My server's having a bit of an issue): what if you broadcasted the victim's inventory? If it's set, you could set a variable to "%victim's inventory%" and check if that variable contains "iron ingot"? Of course, it'd trip if any item names or lores contain "iron ingot", but it's an option if "victim's inventory contains iron ingot:" didn't work.
 
Here's an idea (and again, I'm seriously sorry that I can't test stuff right now. My server's having a bit of an issue): what if you broadcasted the victim's inventory? If it's set, you could set a variable to "%victim's inventory%" and check if that variable contains "iron ingot"? Of course, it'd trip if any item names or lores contain "iron ingot", but it's an option if "victim's inventory contains iron ingot:" didn't work.
How would I do what your saying?
 
How would I do what your saying?
code_language.skript:
set {_inv} to "%victim's inventory%"
broadcast {_inv} # for debugging purposes, mainly
if {_inv} contains "iron ingot":
Alternatively,
code_language.skript:
set {_inv::*} to split "%victim's inventory%" at ", " and " and "
loop {_inv::*}:
  subtext of loop-value from character 1 to character 11 is "iron ingot" # 1-11 or 0-10. i can't remember
  # run code
  exit loop
The second is a little bit safer against item names and lores.

I want to say, just for clarification, that this is not what you should ever do in most situations. This is kind of a last-ditch effort if it's saying the victim doesn't have any iron when they do.

Edit: Ever since last night I haven't been thinking fully. Do not do the second option, it won't work for multiple items. What's your Skript version, Minecraft version, and Skript addons? I'll find a solution for you.
 
Last edited:
code_language.skript:
set {_inv} to "%victim's inventory%"
broadcast {_inv} # for debugging purposes, mainly
if {_inv} contains "iron ingot":
Alternatively,
code_language.skript:
set {_inv::*} to split "%victim's inventory%" at ", " and " and "
loop {_inv::*}:
  subtext of loop-value from character 1 to character 11 is "iron ingot" # 1-11 or 0-10. i can't remember
  # run code
  exit loop
The second is a little bit safer against item names and lores.

I want to say, just for clarification, that this is not what you should ever do in most situations. This is kind of a last-ditch effort if it's saying the victim doesn't have any iron when they do.

Edit: Ever since last night I haven't been thinking fully. Do not do the second option, it won't work for multiple items. What's your Skript version, Minecraft version, and Skript addons? I'll find a solution for you.
Minecraft version is 1.20.2, Skript version is 2.7.2. I have the following addons: Skbee, skript-reflect, skRayFall, SharpSk & SkQuery.