RemoteFunction returning nil

This remotefunction returns a nil value despite the fact that I specified it to return a vector3. Why is this? The error is: Attempt to index nil with “position” line 8. Server script. I printed raycastResult on the server script and it printed nil. I printed it on the client script and it successfully printed the vector3 I wanted. I left most of the client script out to keep things simpler as it works fine.

Client:

		UIS.InputBegan:Connect(function(input)
				if input.UserInputType == Enum.UserInputType.MouseButton1 then
					wait()
					remoteEvent:FireServer()
					remoteFunction.OnClientInvoke = function(player)
						return raycastResult
					end
					IsSpawned = true
					militiaclone:Destroy()
				end
			end)
		end
	end
end)

Server:

remoteEvent.OnServerEvent:Connect(function(player)
	local remoteFunction = game:GetService("ReplicatedStorage").GetMilitiaCloneRemoteFunction
	local raycastResult = remoteFunction:InvokeClient(player)
	local militiaclone = game:GetService("ReplicatedStorage").Militia:Clone()
	local x = raycastResult.Position.X
	local y = raycastResult.Position.Y + 2
	local z = raycastResult.Position.Z
	local rayCFrame = CFrame.new(x, y, z)
	militiaclone:SetPrimaryPartCFrame(rayCFrame)
	militiaclone.Parent = workspace
	militiaclone:WaitForChild("MilitiaScript").Disabled = false
end)

You can’t send RaycastResult through RemoteFunctions due to paramter limitations, what u shud b doing is sending the raycastResult.Instance but make sure the Instance it hit wasn’t created in the client side or only visible to client.

1 Like

Thank you very much. I am dragging this reply out because of limits.

1 Like

It looks like you should send your data as part of the remote event instead of waiting on a client function; it could save you some headaches and script stalling. I’m pretty sure you can send Vector3 over remote events

-- client script
if input.UserInputType == Enum.UserInputType.MouseButton1 then
	remoteEvent:FireServer(raycastResult.Position)

-- server script
remoteEvent.OnServerEvent:Connect(function(player: Player, raycastPosition: Vector3)
	local militiaclone = game:GetService("ReplicatedStorage").Militia:Clone()
	local x = raycastPosition.X
	local y = raycastPosition.Y + 2
	local z = raycastPosition.Z
	local rayCFrame = CFrame.new(x, y, z)
2 Likes

Yes, thank you that is true as I found out.