Second argument to FireServer is always a player

I’m currently working with keybinds near an object and it’s alright, however, there’s an issue when the second argument I fire to the server is always a player.

On the client, I’m detecting the closest part to the player so that they can interact with the part via ContextActionService.

When I print the closest part on the client, it’s the Part Instance that is close to the player, however, on the server, the closest part is the player themselves.

How come the server is doing this behavior? Did I unintentionally mess up the second argument myself?

-- client script
local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")
local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Player = Players.LocalPlayer
local Character = Player.CharacterAdded:Wait()

local Event = ReplicatedStorage:WaitForChild("Event")

local Range = 12
local E_KEYBIND_NAME = "PressE"
local ClosestPart

local function Handle(name, state, input)
	if state == Enum.UserInputState.Begin then
		if name == E_KEYBIND_NAME then
			print('Player pressed E and collected a '..ClosestPart.Name)
			Event:FireServer(Player, ClosestPart)
		end
	end
end

RunService.Heartbeat:Connect(function()
	local Tagged = CollectionService:GetTagged("Interactive Objects")
	
	for _, part in ipairs(Tagged) do
		local Distance = (part.Position - Character.HumanoidRootPart.Position).magnitude

		if Distance <= Range then
			ClosestPart = part

			ContextActionService:BindAction(E_KEYBIND_NAME, Handle, true, Enum.KeyCode.E)
			print('Player is near '..ClosestPart.Name)
			return
		end

		ContextActionService:UnbindAction(E_KEYBIND_NAME)
		ClosestPart = nil
	end
end)
-- server script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local CollectionService = game:GetService("CollectionService")

local Event = ReplicatedStorage:WaitForChild("Event")

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local BoxesCollected = Instance.new("IntValue")
	BoxesCollected.Name = "Boxes"	
	BoxesCollected.Parent = leaderstats
	BoxesCollected.Value = 0
end)

Event.OnServerEvent:Connect(function(player, closestPart)
	print(closestPart)
end)

The first argument for OnServerEvent is always the player who fired the event, so you don’t need to pass the player as an argument in FireServer:

Event:FireServer(ClosestPart)

If you do Event:FireServer(Player, ClosestPart), then the parameters in OnServerEvent would be:

Event.OnServerEvent:Connect(function(player, player, closestPart)
3 Likes

the first argument returned in OnServerEvent is the player that fired the function by default. You do not need to send the player instance in the FireServer function.