Remote event call not passing arguments

I’ve looked on the official docs and Google and I tried their suggestions and nothing is working for me. The RemoteEvent itself works fine and calls the appropriate server function, but it’s completely ignoring the second argument I give it which is a raycastResult (I’m aware that remote function calls implicitly pass player as the first argument, even if it’s not given).

Client code:

    local character = LocalPlayer.Character
	local humanoid = character:WaitForChild("Humanoid")
	
	local rayOrigin = humanoid.RootPart.CFrame.Position
	local rayDirection = game.Workspace.CurrentCamera.CFrame.LookVector * 50
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
	
	local fireBeamEvent = ReplicatedStorage:WaitForChild("FireBeam")
	fireBeamEvent:FireServer(raycastResult)	

Server code:

local fireBeamEvent = ReplicatedStorage:WaitForChild("FireBeam")

    local function fireBeam(raycastResult)
        print(raycastResult.Instance)
    end

fireBeamEvent.OnServerEvent:Connect(fireBeam)

As you can see I am passing a raycastResult argument, but when the game actually goes to run the fireBeam functions, it gives this error:

19:19:13.404 - Instance is not a valid member of Player "Players.CrimsonBakuretsu

It’s pretending as if I said print(player.Instance) while ignoring the raycastResult. I’ve tried both explicitly passing player in the arguments as well as not, following this thread (How to properly pass info through arguments with RemoteEvents (LocalScript to ServerScript)), and the suggestions given in that thread did not work.

I have tried calling raycastResult.Instance on the client and it works fine there, it just gets messed up when passed to a remote function. The documentation on raycastResult does not state it cannot be replicated, so there’s no reason it shouldn’t be able to pass through to the server.

Thanks in advance.

The first argument always on a remote event that is fired to the server is the player that called the remote event.

You should change
local function fireBeam(raycastResult)

To
local function fireBeam(player, raycastResult)

Also make sure if that raycastResult is equal to nil, that you do not fire the remote event so you have no errors thrown in the output.

Hello,

Thank you for your response. I have put player as the first argument in fireBeam and it gets rid of the player error, but unfortunately it does still give me the error that I’m indexing “nil” with instance, even when the raytrace is indeed hitting something. I have surrounded my function call with the following nil check:

if raycastResult ~= nil then
		local fireBeamEvent = ReplicatedStorage:WaitForChild("FireBeam")
		fireBeamEvent:FireServer(raycastResult)	
	end

And it still somehow gets through and allows nil errors, despite checking against it.

When I print the raycastResult in client it does properly report the object it hits.

There are some complexities when sending a dictionary to the server. In this case, the dictionary is raycastResult. Because of this, all the information in raycastResult is lost when it is sent to the server. It’s best to send the actual part that you hit, instead of sending a dictionary.

As such change:
fireBeamEvent:FireServer(raycastResult)
To
fireBeamEvent:FireServer(raycastResult.Instance)

And

    local function fireBeam(player, raycastResult)
        print(raycastResult.Instance)
    end

To

    local function fireBeam(player, hit)
        print(hit)
    end
1 Like

Oh my goodness thank you! I absolutely had no idea dictionaries dropped information when being passed to a server. I just thought that as long as the docs didn’t say it couldn’t be replicated that it was fine. There are so many subtleties to get tripped up on. Out of curiosity, is there any reason why Roblox does things this way?

Thanks again for your help.

1 Like

I don’t know why Roblox does this, maybe it’s for safety reasons, or maybe it takes too much power to push dictionaries across boundaries. There’s so many unknowns about the Roblox engine for me, but I glad I could help you.

You can mark my post as a solution so if other users have this problem they can know and see the solution faster.