Updating an Ammo Counter - Unable to cast value to Object

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I want to pass MAGAZINETOTAL to the client so that it can update a ammo counter in the player’s GUI.

  2. What is the issue?
    Anytime I shoot my gun, the console prints the error “Unable to cast value to Object” and the ammo counter does not update.

  3. What solutions have you tried so far?
    I have looked on the Dev hub here, but some of them didn’t really help me understand how to solve this issue. I have tried tostring as shown in the code, but it still prints the same error.

If it’s the case that this method of trying to count ammo on the player’s GUI is not the best, then kind of method should I use?

For context, I am using the FastCast module for my gun and I’m a fairly new to coding in Luau.

Server Script for my gun:

local reloading = false
local ammoCEvent = RS.Remotes:WaitForChild("AmmoCEvent")

MouseEvent.OnServerEvent:Connect(function (clientThatFired, mousePoint) --Firemodes come from here
	if not CanFire then
		return
	end
	
	CanFire = false
	
	if MAGAZINETOTAL ~= 0 then
		local mouseDirection = (mousePoint - FirePointObject.WorldPosition).Unit
		for i = 1, BULLETS_PER_SHOT do
			Fire(mouseDirection)
		end

		if FIRE_DELAY > 0.03 then
			if MAGAZINETOTAL <= 30 and MAGAZINETOTAL > 0 and not reloading then
				MAGAZINETOTAL = MAGAZINETOTAL - 1
				wait(FIRE_DELAY)
			elseif MAGAZINETOTAL <= 0 then
				MAGAZINETOTAL = 0
				return
			end
			print("Magazine total is:", MAGAZINETOTAL)
			
		end
	end
	CanFire = true
	ammoCEvent:FireClient(tostring(MAGAZINETOTAL)) --Error code directs me here
end)

local requestingReload = false
local currentlyReloading = false

Local Script for the GUI:

local Players = game:GetService("Players")

local Plr = Players.LocalPlayer

local PlayerGui = Plr:WaitForChild("PlayerGui")

local ScreenHUD = game.StarterGui:WaitForChild("HUD")

local RS = game:GetService("ReplicatedStorage")

local ammoCEvent = RS:WaitForChild("Remotes"):WaitForChild("AmmoCEvent")

local ammoCount = script.Parent.Info.GunInfo:WaitForChild("AmmoCount")

ammoCEvent.OnClientEvent:Connect(function(plr, ammoRecorded)

print(ammoRecorded)

ammoCount.Text = ammoRecorded .. "/30"

end)

Hello , i think i might have found the issue when using ammoCEvent:FireClient() you need to type wich player you’re firing the remote event someting like this ammoCEvent:FireClient(clientThatFired,MAGAZINETOTAL) should work however if you want to fire to all clients you can use ammoCEvent:FireAllClients()

1 Like

Thanks man, that solved the “Unable to cast” error, but it seems MAGAZINETOTAL is nil when I try and update the counter. Any ideas?

I get this error: “Players.ricefield_man.PlayerGui.HUD.HUDScript:12: attempt to call a nil value”

Does this line prints magazinetotal ?

1 Like

Don’t have “plr”. Server does not pass the player through to the client when firing the client with its initial call.

Just remove “plr”.

1 Like

Yes it prints the number of rounds.

Yes this should fix the problem :slight_smile: do what lametta said

1 Like

Ok, that fixed the nil issue, thanks.

I got a new error: “Players.ricefield_man.PlayerGui.HUD.HUDScript:12: attempt to call a number value”

Try using tostring and see if it works. tostring(ammoRecorded)…“/30”

1 Like

Works perfectly now, thanks so much @faze_paspro and @Lametta!

2 Likes

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