Gun with ammo and max ammo not working with local variable

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

wajiless

Member
Jan 16, 2025
1
0
1
24
i made multiple guns then released that i had the same ammo for all of them so i made them local then it didnt work anymore.

code_language.skript:
on load:
    {_max_ammo} = 30
    {_ammo} = 30
    

on leftclick:
    if name of player's held item is "&f&lDesert Eagle":
        send action bar "Ammo left: %{_ammo}%" to player
        if {_ammo} is greater than 1:
            make the player shoot a arrow at speed 20
            subtract 1 from {_ammo}   
        else:
            send "DEBUG: NO AMMO" to player
            send "DEBUG: AMMO VARIABLE: %{_ammo}%" to player
        

on rightclick:
    if name of player's held item is "&f&lDesert Eagle":
        add 15 to {_ammo}   
        send action bar "Ammo left: %{_ammo}%" to player
        if {_ammo} is greater than {_maxammo}:
            set {_ammo} to {_maxammo}   
            send action bar "Ammo left: %{_ammo}%" to player
 
i made multiple guns then released that i had the same ammo for all of them so i made them local then it didnt work anymore.

code_language.skript:
on load:
    {_max_ammo} = 30
    {_ammo} = 30
   

on leftclick:
    if name of player's held item is "&f&lDesert Eagle":
        send action bar "Ammo left: %{_ammo}%" to player
        if {_ammo} is greater than 1:
            make the player shoot a arrow at speed 20
            subtract 1 from {_ammo}  
        else:
            send "DEBUG: NO AMMO" to player
            send "DEBUG: AMMO VARIABLE: %{_ammo}%" to player
       

on rightclick:
    if name of player's held item is "&f&lDesert Eagle":
        add 15 to {_ammo}  
        send action bar "Ammo left: %{_ammo}%" to player
        if {_ammo} is greater than {_maxammo}:
            set {_ammo} to {_maxammo}  
            send action bar "Ammo left: %{_ammo}%" to player
Local means that it is only saved in the expression you write it in so even if you put in an on load statement, it's not gonna save that value. Using global variables like before was the better option. You could also always delete them after use as well.
 
Hey,
{_max_ammo} only works on functions or events. That is a temopary Variable, so you need to use a variable without the underline at the start.
Also the variable you are trying to use, is a server variable, which affects every player on the server,
 
I can give you my gun skript:

change it to your liking:
Code:
command /gun <text>:
    permission: op
    trigger:
        if argument 1 is "ak47" or "ak-47":
            give player iron horse armor named "&6AK-47" with lore "&c&lCONTRABAND" with all item flags
        else if argument 1 is "akammo":
            give player 16 white dye named "&7AK-47 Ammo" with lore "&c&lCONTRABAND"
        else if argument 1 is "sniper":
            give player diamond horse armor of curse of vanishing named "&6Sniper" with lore "&c&lCONTRABAND" with all item flags
        else if argument 1 is "sniperammo":
            give player 16 black dye named "&7Sniper Ammo" with lore "&c&lCONTRABAND"
        else if argument 1 is "minigun":
            give player gold horse armor of curse of vanishing named "&8[&705&8] &6Minigun" with lore "&c&lCONTRABAND" with all item flags
        else:
            send "&7Please specify what you want? (&6ak47&7, &6akammo&7, &6sniper&7, &6sniperammo &7or &6minigun&7?)" to player
        
on right-click:
    if player is holding iron horse armor named "&6AK-47":
        if player has white dye named "&7AK-47 Ammo":
            shoot arrow at speed 2
            remove 1 white dye named "&7AK-47 Ammo" from player
        else:
            send "&cGet some ammo bud!" to player
    if player is holding diamond horse armor named "&6Sniper":
        if player has black dye named "&7Sniper Ammo":
            if {snipercooldown.%player%} = true:
                send "&cReloading!" to player
            else:
                set {snipercooldown.%player%} to true
                shoot arrow at speed 10
                remove 1 black dye named "&7Sniper Ammo" from player
                wait 10 seconds
                set  {snipercooldown.%player%} to false
        else:
            send "&cGet some ammo bud!" to player
    else if player is holding gold horse armor of curse of vanishing named "&8[&705&8] &6Minigun":
        if player has 1 white dye named "&7AK-47 Ammo":
            remove 1 white dye named "&7AK-47 Ammo" from player
            shoot arrow at speed 4
            wait 0.1 seconds
            shoot arrow at speed 4
            wait 0.1 seconds
            shoot arrow at speed 4
        else:
            send "&cGet some ammo bud!" to player