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

TimeStellar

Member
Mar 4, 2024
19
0
1
24
I have a weapon level system but i dont understand why it doesnt work like when i right click (i set it so for testing) it gives xp but the max xp breaks and goes to none can anyone help?

Code:
on right click with a sword:
    if name of player's tool is "&aLeveled Sword":
        
        set {_lore} to uncolored line 1 of lore of player's tool
        
        
        set {_level_text::*} to {_lore} split at " "
        set {_level} to {_level_text::2} parsed as number
        
        
        set {_xp_section} to {_level_text::3}  # This is "(0/100 XP)"
        set {_xp_section::*} to {_xp_section} split at "/"
        set {_current_xp} to {_xp_section::1} parsed as number
        set {_max_xp_text} to {_xp_section::2} split at " "  # Split to remove " XP"
        set {_max_xp} to {_max_xp_text::1} parsed as number  # Parse the number


        
        
        add 10 to {_current_xp}
        
        
        if {_current_xp} >= {_max_xp}:
            subtract {_max_xp} from {_current_xp}  # Carry over excess XP
            add 1 to {_level}  # Increase the level
            set {_max_xp} to {_max_xp} * 2  # Double the max XP required for the next level
            send "Your sword leveled up to level %{_level}%!" to player
            
        set line 1 of lore of player's tool to "Level %{_level}% (%{_current_xp}%/%{_max_xp}% XP)"
        send "XP: %{_current_xp}%/%{_max_xp}%, Level: %{_level}%" to player
 
I have a weapon level system but i dont understand why it doesnt work like when i right click (i set it so for testing) it gives xp but the max xp breaks and goes to none can anyone help?

Code:
on right click with a sword:
    if name of player's tool is "&aLeveled Sword":
       
        set {_lore} to uncolored line 1 of lore of player's tool
       
       
        set {_level_text::*} to {_lore} split at " "
        set {_level} to {_level_text::2} parsed as number
       
       
        set {_xp_section} to {_level_text::3}  # This is "(0/100 XP)"
        set {_xp_section::*} to {_xp_section} split at "/"
        set {_current_xp} to {_xp_section::1} parsed as number
        set {_max_xp_text} to {_xp_section::2} split at " "  # Split to remove " XP"
        set {_max_xp} to {_max_xp_text::1} parsed as number  # Parse the number


       
       
        add 10 to {_current_xp}
       
       
        if {_current_xp} >= {_max_xp}:
            subtract {_max_xp} from {_current_xp}  # Carry over excess XP
            add 1 to {_level}  # Increase the level
            set {_max_xp} to {_max_xp} * 2  # Double the max XP required for the next level
            send "Your sword leveled up to level %{_level}%!" to player
           
        set line 1 of lore of player's tool to "Level %{_level}% (%{_current_xp}%/%{_max_xp}% XP)"
        send "XP: %{_current_xp}%/%{_max_xp}%, Level: %{_level}%" to player
I would send out the values the variables are changing to so you can debug it (Sending the variable values before and after adding/doing math with em) and that may help you find out the source of the problem.
 
Code:
on right click with a sword:
    if name of player's tool is "&aLeveled Sword":
        
        set {_lore} to uncolored line 1 of lore of player's tool
        
        # Split the level and XP section
        set {_level_text::*} to {_lore} split at " "
        
        # Extract the level from the lore
        set {_level} to {_level_text::2} parsed as number
        
        # Extract the XP section from the lore (this should be "(0/100 XP)")
        set {_xp_section} to {_level_text::3}  # "0/100 XP"
        set {_xp_section::*} to {_xp_section} split at "/"
        
        # Safely parse the current XP and max XP
        if {_xp_section::1} is set and {_xp_section::2} is set:
            set {_current_xp} to {_xp_section::1} parsed as number
            set {_max_xp_text} to {_xp_section::2} split at " "  # Remove " XP"
            set {_max_xp} to {_max_xp_text::1} parsed as number
        else:
            send "Error: Could not parse XP section. Please check the sword's lore." to player
            stop trigger
        
        # Add XP
        add 10 to {_current_xp}
        
        # Level up if the current XP exceeds or equals the max XP
        if {_current_xp} >= {_max_xp}:
            subtract {_max_xp} from {_current_xp}  # Carry over excess XP
            add 1 to {_level}  # Increase the level
            set {_max_xp} to {_max_xp} * 2  # Double the max XP required for the next level
            send "Your sword leveled up to level %{_level}%!" to player
            
        # Update the lore with the new level and XP
        set line 1 of lore of player's tool to "Level %{_level}% (%{_current_xp}%/%{_max_xp}% XP)"
        send "XP: %{_current_xp}%/%{_max_xp}%, Level: %{_level}%" to player
The issue lies in how the script handles parsing and updating the XP section of the sword's lore. Specifically, the split and parsing steps might not always account for special cases (like missing or misformatted XP sections), which could cause the "max XP" value to break.
 
1726316890300.png
 
Try This
Code:
on right click with a sword:
    if name of player's tool is "&aLeveled Sword":
        
        set {_lore} to uncolored line 1 of lore of player's tool
        
        # Split the level and XP section
        set {_level_text::*} to {_lore} split at " "
        
        # Extract the level from the lore
        set {_level} to {_level_text::2} parsed as number
        
        # Extract the XP section from the lore (this should be "(0/100 XP)")
        set {_xp_section} to {_level_text::3}  # "0/100 XP"
        set {_xp_section::*} to {_xp_section} split at "/"
        
        # Safely parse the current XP and max XP
        if {_xp_section::1} is set:
            {_xp_section::2} is set
            set {_current_xp} to {_xp_section::1} parsed as number
            set {_max_xp_text::*} to {_xp_section::2} split at " "  # Remove " XP"
            set {_max_xp} to {_max_xp_text::1} parsed as number
        else:
            send "Error: Could not parse XP section. Please check the sword's lore." to player
            stop trigger
        
        # Add XP
        add 10 to {_current_xp}
        
        # Level up if the current XP exceeds or equals the max XP
        if {_current_xp} >= {_max_xp}:
            subtract {_max_xp} from {_current_xp}  # Carry over excess XP
            add 1 to {_level}  # Increase the level
            set {_max_xp} to {_max_xp} * 2  # Double the max XP required for the next level
            send "Your sword leveled up to level %{_level}%!" to player
            
        # Update the lore with the new level and XP
        set line 1 of lore of player's tool to "Level %{_level}% (%{_current_xp}%/%{_max_xp}% XP)"
        send "XP: %{_current_xp}%/%{_max_xp}%, Level: %{_level}%" to player
 
i think i know why when i set the lore xp to 30 and right clicked it was 10 again but i think that it does that because the value is zero and then it adds 10 and next time it adds 10 but resets to zero so i think getting the xp from lore is the reason