i’m very tired i’m gonna do my best to explain this
basically;
i have a textlable that displays the current ammo you have, along with the maximum amount of it
the current ammo gets updated properly, but for whatever reason the maximum amount doesn’t get updated until the gun gets fired after reloading (annoying)
additionally, i clone a textlabel from replicated storage and put it under the player’s crosshair/mouse, then flash it, very cool QoL!!
visual presentation:
and this is the code,
function OnBulletsAmountChanged(newAmount: number, lowBulletsNumber: number, weapon: Tool)
local ammoTypeString: string = weaponInfo[weapon.Name]["AMMO_TYPE"]
local ammoTypeValue: IntValue = plr:WaitForChild(ammoTypeString)
ammoCounter.Text = newAmount .. "/" .. ammoTypeValue.Value
print(ammoTypeValue.Value) -- Is always 99999 until the gun is fired again... ugh
local flashingReloadText = weaponGui:FindFirstChild("FlashingReloadText")
if newAmount <= lowBulletsNumber then
local flashingReloadText = rStorage:FindFirstChild("FlashingReloadText"):Clone()
local mousePos = UDim2.new(mouse.X, 0, mouse.Y - 2, 0)
flashingReloadText.Parent = weaponGui
flashingReloadText.Position = mousePos
coroutine.wrap(function()
FlashText(flashingReloadText) -- this is just a while loop that flashes the text (that isn't visible!)
end)()
ammoCounter.TextColor3 = Color3.fromRGB(255,0,0)
else
ammoCounter.TextColor3 = Color3.fromRGB(255,255,255)
if not flashingReloadText then return end
flashingReloadText:Destroy()
end
end
function OnToolEquipped(weapon: Tool)
if not weapon:IsA("Tool") then return end
local weaponType: string = weaponInfo[weapon.Name]["WEAPON_TYPE"]
local descriptionToDisplay: string = weaponInfo[weapon.Name]["DESCRIPTION"]
local nameToDisplay: string = weapon:GetAttribute("Name")
local flashingReloadText = weaponGui:FindFirstChild("FlashingReloadText")
if flashingReloadText then
flashingReloadText:Destroy()
end
DisplayWeaponTypeIcon(weaponType)
-- Displaying both weapon name and description before any if statements
weaponName.Text = nameToDisplay
weaponDescription.Text = descriptionToDisplay
main.Visible = true
if weaponType == "GUN" then
-- Here we retrieve the needed information!
local lowBulletsNumber: number = weaponInfo[weapon.Name]["LOW_BULLETS_NUMBER"]
print(lowBulletsNumber)
local weaponFiringType: string = weaponInfo[weapon.Name]["FIRING_TYPE"]
local weaponAmmoType: string = weaponInfo[weapon.Name]["AMMO_TYPE"]
local weaponBullets: IntValue = weapon:FindFirstChild("Bullets")
local playerAmmoTypeValue: IntValue = plr:FindFirstChild(weaponAmmoType)
local ammoAmountToDisplay: string = weaponBullets.Value .. "/" .. playerAmmoTypeValue.Value
ammoCounter.Text = weaponBullets.Value
OnBulletsAmountChanged(weaponBullets.Value, lowBulletsNumber, weapon) -- Just so the text starts flashing
-- Once the player equips the weapon and it's on low ammo
DisplayAmmoTypeIcon(weaponAmmoType)
DisplayFiringTypeIcon(weaponAmmoType)
weaponBullets.Changed:Connect(function(newAmount)
OnBulletsAmountChanged(newAmount, lowBulletsNumber, weapon)
end)
-- Not showing more as it doesn't break anything
I was talking about ammoTypeValue.Value, so for example: ammoTypeValue.Value = 123
I mentioned it because since it’s not being set in the script provided, I felt there was a chance that you might’ve accidentally forgotten to do so, which would explain why its value isn’t changing
I didn’t really explain myself correctly in the previous edit: What I meant to say was that ammoTypeValue.Value isn’t being set at the correct moment, which is before the counter is updated, and would explain why its value is changing after the gun is fired rather than after reloading is complete. The onReload function in my reply below shows what I’m talking about
Try passing the ammoTypeValue (the IntValue) to the function that handles reloading, and after reloading is complete, set its value to: ammoTypeValue.Value - theTotalAmmoAfterReloading
Essentially doing something like this should work if you make sure to call the function after the gun has finished reloading:
local function onReload(newAmount: number, ammoTypeValue: IntValue)
ammoTypeValue.Value -= newAmount -- Remove the bullets from the magazine
ammoCounter.Text = newAmount .. '/' .. ammoTypeValue.Value -- Set the counter's text
print(ammoCounter.Text) -- Print the counter's text for debugging
end
If the server is never updating the value, then check to make sure that it’s updating the GUI inside of the player’s PlayerGui rather than the one stored inside of StarterGui
It’s a common mistake, which is why I wanted to check to make sure
I usually use a RemoteFunction rather than a RemoteEvent to handle reloading: The client invokes the server, and the server returns the new total amount of ammo in the magazine. The client-side script will yield until it receives the value from the server, and the client updates their ammo counter accordingly. Essentially this means that the client is handling their GUI, not the server, but the server is still keeping track of their current ammo for security
TLDR: I personally use a gun system that’s very different from the one you’re using, so my recommendations might not always be directly compatible with your gun system, at least without major modifications