FireClient: player argument must be a player object?

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

Try printing the ‘player’ variable on both the server and the client to make it sure prints the player.

1 Like

still havent figured it out, nevermind

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.

1 Like

it prints nil?

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

remove : Player as you know it’ll be a player object (It’s Hard Coded).
Can we see the code where it’s calling this local function?

1 Like

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.

1 Like

Server-side (reload function and connection)

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)

the code is listed above (character limit)

reloadRemote.OnServerEvent:Connect(function(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)

Try this maybe?

1 Like

Ah. I figured it out @Youtub5005 @SeasonedRiceFarmer

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

Sorry for the confusion, thanks for your help.

1 Like

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