Bullet amount doesn't get updated properly unless the gun gets shot, additionally the flashing reload text is NOT visible

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

mouse.X and mouse.Y are offset values, and you’re currently using them as scale values, which explains why the flashingReloadText GUI isn’t showing up

This:

should be this:

local mousePos = UDim2.fromOffset(mouse.X, mouse.Y - 2)

I’ve yet to figure out what’s causing the other problem, though


@notsad2

The value of ammoTypeValue doesn’t seem to be set anywhere in the script provided

1 Like

image
?

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


@notsad2

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

that’s because it takes the current ammo the player has of weapon that’s being used

by default (right now), it is 99999

Try passing the ammoTypeValue (the IntValue) to the function that handles reloading, and after reloading is complete, set its value to: ammoTypeValue.Value - theTotalAmmoAfterReloading


@notsad2

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

…yeah that’s handled on the server

horrible code? maybe
image


the problem for some reason is that the server updates the value, but the client doesn’t…
image

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

well duuuh

i’m not stupid!

image

also that onReload function you gave me would be kind of annoying to put into that code
are you sure you don’t know any other solutions :confused:

It’s a common mistake, which is why I wanted to check to make sure :sweat_smile:

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

sigh…

i had to use a remote event for the ammo count
i really wish i didn’t have to, but i don’t care enough to actually look into this [damn] problem

thanks for your efforts though

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.