RemoteEvent Seems to Forget Parameter Value

My scripts are returning an error when they are activated, and I can’t seem to find out why. It seems like the LocalScript is able to print the “target” parameter with no problem, but the ServerScript seems to forget the value of the parameter, and only prints “nil”.

Error:
image

Local:

local repStorage = game:GetService("ReplicatedStorage")
local aimEvent = repStorage:WaitForChild("AimedEvent")

local playerService = game:GetService("Players")
local player = playerService.LocalPlayer
local mouse = player:GetMouse()

local handle = script.Parent
local tool = handle.Parent

mouse.Button1Down:Connect(function(players, target, char, hum)
	local char = player.Character
	local hum = char:WaitForChild("Humanoid")
	
	if mouse.Target then
		if mouse.Target.Parent:FindFirstChildOfClass("Humanoid") then
			target = mouse.Target
			print(target.Parent)
			aimEvent:FireServer(players, target, char, hum)
		else
			print("Target Does Not Contain Humanoid")
		end
	end
end)

Server:

local repStorage = game:GetService("ReplicatedStorage")
local aimEvent = repStorage:WaitForChild("AimedEvent")

aimEvent.OnServerEvent:Connect(function(players, target, char, hum)
	print(target)
	
	local bodyGyro = Instance.new("BodyGyro")
	bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
	bodyGyro.D = 0
	bodyGyro.CFrame = char.HumanoidRootPart.CFrame
	bodyGyro.Parent = char.HumanoidRootPart
	
	local targetCFrame = CFrame.lookAt(char.HumanoidRootPart.Position, target:WaitForChild("HumanoidRootPart").Position)
	char.HumanoidRootPart.BodyGyro.CFrame = targetCFrame
end)


Any help is greatly appreciated

When you fire a RemoteEvent to the Server, it will only apply arguments to the second parameter, and so on, when firing a RemoteEvent to the Server, it will already know which Player it came from, so you can remove the Player argument from FireServer

2 Likes

I’m a bit new to RemoteEvents and functions, mind explaining this a bit more in depth? I wasn’t aware there was a difference between args and params

As @DasKairo said, the first parameter of .OnServerEvent is always the player calling the RemoteEvent

For example your code should look like this:

aimEvent.OnServerEvent:Connect(function(player, players, target, char, hum) -- player being the player who called the RemoteEvent
1 Like

This is basically the OnServerEvent Connection:

func: (player: Player, ...: any)

The First Parameter will always be the Player when firing to the Server, but the rest are a list that is given from your code (aka your own arguments), The Server will already know what Player the RemoteEvent is from, and it will use all other Arguments, as far as the function is concerned, you specified that the Target Parameter is a Player, which you dont want it to be.

-- Our Arguments: (target, character, humanoid)

-- FireServer (player, target, character, humanoid)
-- Given Data (player, target, character, humanoid)
-- Result (target = player, character = target, humanoid = character)

-- The Server will already be aware of the Player, so you dont need to specify them


-- FireServer (player, target, character, humanoid)
-- Given Data (target, character, humanoid)
-- Result (target = target, character = character, humanoid = humanoid)

The only time you have to Specify what Player is need is when you telling the Server to send Data to the Client, where you would need to Specify which Client to send to.

1 Like

Thank you both, I was under the impression that “Players” could take the place of “Player” :sweat_smile:

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