I’m working on a gun system and im getting this error? FireClient: player argument must be a player object I cant figure out why is this happening when I already have the “valid” player object from the remote event?
local function onReloadEvent(player: Player)
if not canReload() then
return
end
isReloading = true
sounds.Reload:Play()
task.delay(Constants.RELOAD_TIME, function()
isReloading = false
ammoValue.Value = Constants.MAGAZINE_SIZE
remotes.UpdateAmmoGui:FireClient(player, ammoValue.Value, Constants.MAGAZINE_SIZE)
end)
end
Also note, that when the server is receiving a remote event.
RemoteEvent.OnClientEvent:Connect(function(Player) end) – Where player is always going to be a player object
:FireClient(player) – Firing a remote eventfrom the server you MUST include a valid player object as the first argument.
local function onReloadEvent(player: Player): ()
print(player)
if not canReload() then
return
end
isReloading = true
sounds.Reload:Play()
task.delay(Constants.RELOAD_TIME, function()
isReloading = false
ammoValue.Value = Constants.MAGAZINE_SIZE
remotes.UpdateAmmoGui:FireClient(player, ammoValue.Value, Constants.MAGAZINE_SIZE)
end)
end
Also one thing I noticed, the script only breaks when my ammo is 0? I dont know if this is just a coincidence or something
You’re gonna have to backtrace even further to figure out why its not being properly passed to your onReloadEvent function. We can’t really help you further without know more about the flow of your code.
local function onReloadEvent(player: Player)
if not canReload() then
return
end
isReloading = true
sounds.Reload:Play()
task.delay(Constants.RELOAD_TIME, function()
isReloading = false
ammoValue.Value = Constants.MAGAZINE_SIZE
remotes.UpdateAmmoGui:FireClient(player, ammoValue.Value, Constants.MAGAZINE_SIZE)
end)
end
reloadRemote.OnServerEvent:Connect(onReloadEvent)
Client-side (the function that triggers the reload remote event)
local function reload(actionName: string, inputState: Enum.UserInputState)
if actionName ~= "Reload" then
return
end
if inputState == Enum.UserInputState.Begin then
reloadRemote:FireServer()
end
end
ContextActionService:BindAction("Reload", reload, true, Enum.KeyCode.R)
This error was because I had another function that was calling the reload function without passing the player parameter. This is why the reload function didn’t work when the ammo was at zero.
local function onShootEvent(player: Player, mousePosition: Vector3)
-- If the player tries to shoot but has no ammo, then the gun will reload
if ammoValue.Value <= 0 then
onReloadEvent() -- Here
end