Gun Reloads Once

I added notes for myself. In my copy of your file. Sorry, it’s the 2A where the confusing change is made.

Also, I dont understand what the goal of 2B is. It looks like they are giving you more ammo than the original amount, but I’m not sure when that else would ever happen.

Alright. I do have to head to bed soon here, so I’ll get back to you as soon as I can tomorrow.

Any ideas on how I could fix this?

I know how to get around the problem. Replace your reload() function with the following:

local function reload()
script.Parent.MaxAmmo.Value = 30
clipSize = 30
RefreshGui()
canshoot = true
end

The other parts of the function don’t make any sense to me so I just delete them to help get you onto your next project.

There is a wait() somewhere else in the script that causes a delay after reload, so give it a second or two after reloading to make sure it works.

For some reason, it’s not working

It didn’t end up working, anything else?

The code I pasted above worked for me just fine.

There is something wrong in the current relaod() function you are using. It is too confusing for me to try and figure out, but I replaced it with the code above and it worked fine.

There is a delay after reloading, so give it a few second before you decide it isn’t working.

Then find the delay and remove it. :grinning:

could ya download the copy of the game for me and send it back rq?

Can you copy the game and send it back?

It seems like the issue with your gun reloading only once could be related to how you’re managing the ammo and clip size values.

Based on the code you provided, it seems like you’re initializing clipSize to be equal to the current value of script.Parent.Ammo , which I assume represents the number of bullets in the clip. However, you’re never updating the clipSize value after each reload. Instead, you’re updating the Ammo value to represent the total number of bullets the player has left.

To fix this, you could update the clipSize value whenever the player reloads their gun. One way to do this is to subtract the current clip size from the total ammo, and then set clipSize to be the minimum of that result and the maximum clip size (i.e., 30 in your case). Here’s an example of how you could modify your code to achieve this:

local function reload()
    -- Calculate the number of bullets to add to the clip
    local bulletsToAdd = math.min(script.Parent.MaxAmmo.Value, 30) - clipSize
    clipSize = clipSize + bulletsToAdd
    script.Parent.Ammo.Value = script.Parent.Ammo.Value - bulletsToAdd
    gui.Frame.Ammo.Text = clipSize
    gui.Frame.Mag.Text = script.Parent.MaxAmmo.Value - clipSize
end

This function would update the clipSize value and the ammo display in the GUI whenever the player reloads. Note that you’d need to call this function whenever the player presses the reload button (or uses some other method to reload their gun).

You’d also need to make sure that the player can’t shoot their gun if the clipSize value is equal to zero, and that the clipSize value is properly initialized when the player first equips their gun. You may also want to update the GUI to display the current ammo count whenever the player shoots their gun.

Looking at this code, I changed it by adding the part you added. The GUI has started to work; however, when I reload the gun, the output shows the “print(script.Parent.Ammo.Value)” and that output isn’t equal to the ammo value its self. I checked that in the properties. No easy way to explain it.

1 Like