How to parsed number in the item name?

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

AlliSighs

Member
Feb 12, 2024
41
0
6
14
Help me please
Is the code supposed to do:
- 1. it defines the tools (or weapons) that are used by the attacker and the victim during combat
- 2. It then extracts the numeric values from the names of these tools.
- 3. If the attacker's tool is 4 or more units stronger than the victim's tool, the attack is canceled.
BUT! I can hit someone who has 1 lvl sword when I have 9 lvl sword, tools name format: Sword [9] and Sword [1]
```wb
on damage:
set {_attackerItem.%uuid of attacker%} to tool of attacker
set {_victimItem.%uuid of victim%} to tool of victim
set {_attackerNumber.%uuid of attacker%} to first element of split {_attackerItem.%uuid of attacker%}'s name at "[" parsed as integer
set {_victimNumber.%uuid of victim%} to first element of split {_victimItem.%uuid of victim%}'s name at "[" parsed as integer
if {_attackerNumber.%uuid of attacker%} - {_victimNumber.%uuid of victim%} >= 4:
cancel event
send "Your weapon is 4 or more units stronger than the beginner's, you can't hit it!" to attacker
```
 
You can always just retreive the last characters of the name of the attacker's tool, then it's just some simple math! I believe your goal was that if the (attacker's number - 4) is still bigger than the victim's number, it cancels the event.
For example!
AppleScript:
on damage:
    set {_d} to last 4 characters of name of attacker's tool
    set {_v} to last 4 characters of name of victim's tool
    replace all "[" and "]" and " " in {_v} and {_d} with ""
    set {_finalnum} to abs(-4+{_d})
    {_finalnum} > {_v}:
        broadcast "True"
    else:
        broadcast "False"

Keep in mind, the above code only works with up to 2-digit numbers, as it also deletes the space between Sword and the level (just in case its 2 digit and not 3 digit).

If you're going above 2 digit numbers in your item leveling, I'm kind of scared.
All jokes aside, this should return true if the attacker's number is 4 or more units bigger than the victim's. If that does not happen, let me know. I can also break down the code should you need.

A troubleshoot you can attempt to add if the code doesn't work (before coming back here) is adding 2 lines after the text replacing.
set {_d} to ({_d} parsed as integer)
set {_v} to ({_v} parsed as integer)
 
You can always just retreive the last characters of the name of the attacker's tool, then it's just some simple math! I believe your goal was that if the (attacker's number - 4) is still bigger than the victim's number, it cancels the event.
For example!
AppleScript:
on damage:
    set {_d} to last 4 characters of name of attacker's tool
    set {_v} to last 4 characters of name of victim's tool
    replace all "[" and "]" and " " in {_v} and {_d} with ""
    set {_finalnum} to abs(-4+{_d})
    {_finalnum} > {_v}:
        broadcast "True"
    else:
        broadcast "False"

Keep in mind, the above code only works with up to 2-digit numbers, as it also deletes the space between Sword and the level (just in case its 2 digit and not 3 digit).

If you're going above 2 digit numbers in your item leveling, I'm kind of scared.
All jokes aside, this should return true if the attacker's number is 4 or more units bigger than the victim's. If that does not happen, let me know. I can also break down the code should you need.

A troubleshoot you can attempt to add if the code doesn't work (before coming back here) is adding 2 lines after the text replacing.
set {_d} to ({_d} parsed as integer)
set {_v} to ({_v} parsed as integer)
And what if the names of the objects are different, for example,
Viks [1]
Wolfor [2]
Akkkor [3]?
 
Its fine, it just grabs the last characters, so if it was

Sword [4], it'll just get " [4]" and then it deletes the spaces and parenthesis and so its left with "4"

Same with Axe [8], it just gets " [8]" and then "8"

Even Pneumonoultramicroscopicsilicovolcanoconiosos [16] will just return "[16]" (no space this time) and then "16"
 
on damage: set {_d} to last 4 characters of name of attacker's tool set {_v} to last 4 characters of name of victim's tool replace all "[" and "]" and " " in {_v} and {_d} with "" set {_finalnum} to abs(-4+{_d}) {_finalnum} > {_v}: broadcast "True" else: broadcast "False"
I've tested your script, but I get False even when I have an item named "Sword [9]" and the victim has "Sword [1]"
 
I've tested your script, but I get False even when I have an item named "Sword [9]" and the victim has "Sword [1]"
I think I know the problem. Make sure to try broadcasting {_d}, {_v}, and {_finalnum} for testing, and also don't forget to try parsing as an integer.
 
set {_d} to ({_d} parsed as integer)
set {_v} to ({_v} parsed as integer)
I added these lines and it works, but only with the name Sword [n]. It doesn't work with this name:
'{"extra":[{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FD7B00","text":"К"},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FD881A","text":"л"},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FD9533","text":"о"},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FEA34D","text":"в"},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FEB066","text":"н"},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FEBD80","text":"ф"},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FECA99","text":"и"},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FED7B3","text":"ш
"},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FFE5CC","text":"["},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FFF2E6","text":"7"},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FFFFFF","text":"]"}],"text":""}'
 
I added these lines and it works, but only with the name Sword [n]. It doesn't work with this name:
'{"extra":[{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FD7B00","text":"К"},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FD881A","text":"л"},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FD9533","text":"о"},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FEA34D","text":"в"},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FEB066","text":"н"},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FEBD80","text":"ф"},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FECA99","text":"и"},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FED7B3","text":"ш
"},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FFE5CC","text":"["},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FFF2E6","text":"7"},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"#FFFFFF","text":"]"}],"text":""}'
Clownfish [n]
 
Yeah it should only work with normal text, also gotta make sure you have a space between the name of the item and the [num]
 
I've also noticed that if the player has 1 and I also have 1, then we can't hit each other! (
 
Yeah so...I just realized I goofed my math. The absolute value of 1 - 4 is 3 (absolute value turns negative into positive so 1-4 is -3 and then you absolute value and get 3, just in case u forgor abs) just replace the "set {_finalnum} to abs(-4+{_d})" to
AppleScript:
set {_finalnum} to -4+{_d} if {_d} >= 4
set {_finalnum} to 0 if {_d} < 4
 
Yeah so...I just realized I goofed my math. The absolute value of 1 - 4 is 3 (absolute value turns negative into positive so 1-4 is -3 and then you absolute value and get 3, just in case u forgor abs) just replace the "set {_finalnum} to abs(-4+{_d})" to
AppleScript:
set {_finalnum} to -4+{_d} if {_d} >= 4
set {_finalnum} to 0 if {_d} < 4
Thank you, you are the best! And how do I ignore color formatting?
 
Oh you're right I'm a goof. You'll still have to deal with the whole &c and stuff, unless you do something like the following, but the easiest solution is just to grab the last 6 characters of their name, and then manually replace every letter and the & symbol from them.


AppleScript:
set {_e} to name of attacker's tool
replace all "&c" and "&a" and "&b" and "&e" and "&%integers between 0 and 9%" and "&f" and "&r" #repeat for color codes in {_e} with "",
#will have to repeat for alphabet
set {_d} to last 4 characters of {_e}
#continue with the other replacing
#repeat for {_v} with victim's tool

Here's an easy copy and paste for removing the whole alphabet

Code:
replace all "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y" and "z" in {_varname} with ""
 
Oh you're right I'm a goof. You'll still have to deal with the whole &c and stuff, unless you do something like the following, but the easiest solution is just to grab the last 6 characters of their name, and then manually replace every letter and the & symbol from them.


AppleScript:
set {_e} to name of attacker's tool
replace all "&c" and "&a" and "&b" and "&e" and "&%integers between 0 and 9%" and "&f" and "&r" #repeat for color codes in {_e} with "",
#will have to repeat for alphabets
set {_d} to last 4 characters of {_e}
#continue with the other replacing
#repeat for {_v} with victim's tool

Here's an easy copy and paste for removing the whole alphabet

Code:
replace all "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y" and "z" in {_varname} with ""
And what to do if HEX codes are used ?
 
I recommend doing so, that way you just get the number, the closing bracket, and the color code for the closing bracket. For example, if you used &7 and pulled the last 5 digits of the name, you get

[n&7] (for one digit numbers, then you can just replace the &7 and the brackets away)

and

nn&7] (for 2 digit numbers, same logic)
 
I recommend doing so, that way you just get the number, the closing bracket, and the color code for the closing bracket. For example, if you used &7 and pulled the last 5 digits of the name, you get

[n&7] (for one digit numbers, then you can just replace the &7 and the brackets away)

and

nn&7] (for 2 digit numbers, same logic)
But "#FFF00F[#FFF2AF1#FFF412]"