You can write your topic however you want, but you need to answer these questions:
What do you want to achieve?
I want to pass
MAGAZINETOTALto the client so that it can update a ammo counter in the player’s GUI.
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.
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
tostringas 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)