How do I pass in a variable of type RaycastResult as a parameter?

Hey, I am have having a bit of a problem.

To keep things short - I am trying to pass in a variable of type RaycastResult as an argument to a :FireServer() function in a local script. The issue is that when I try to access it’s properties (In a script) it says:
Attempt to index nil with Property Name

He is a screenshot:

Here, I tried to do

print(raycastResult.Instance.Name)

in a local script, and then pass in the raycastResult to the server and try to print it out there, as you can see - it… didn’t work… yea

The questioning template is asking me what solutions I’ve tried so far and honestly - I tried googling for a good amount of time - nothing helped me. I assume, that raycastResult is a table with some stuff in it, so I tried googling “Error passing in a table as an argument Lua” and stuff like that, but the only thing that I found is, drum roll, nothing at all!

So my 2 final questions are:
1.) Is this ok, or am I messing something up?
2.) If it is okay - why is it happening, how do I fix it?
3.) If it is not ok, I can send a full script, it’s not that long, I am just messing around with the new raycasting (The old raycasting: FindPartOnRayWithIgnoreList(), the new one: Rascast())

PS: Thank you to everyone that is going to answer
PS 2: I might not respond immediately, it’s nearly 12 AM for me and I want some sleep

1 Like

My advise to show all code so we can see the implementation and definition, because here it implies that the parameter is nil

2 Likes

The ray doesn’t hit anything. That’s why you are getting an error.
Try doing:

if raycatResult then
   --code
end

Local script (Is inside the StarterCharacterScripts)

-------------
--Variables--
-------------

local player = game.Players.LocalPlayer
local character = player.Character

local rayDistance = 5
local yAmountToTake = 1

-- Setting raycast params
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.IgnoreWater = true
local tableToIgnore = {}
for i, object in pairs(character:GetChildren()) do
	if object:IsA("Part") then
		table.insert(tableToIgnore, object)
	end
end
raycastParams.FilterDescendantsInstances = tableToIgnore

-------------
--Functions--
-------------

while wait(0.01) do
	local newTorsoPosition = Vector3.new(character.Torso.Position.x,
		character.Torso.Position.y - yAmountToTake, 
		character.Torso.Position.z)
	local raycastResult = 
		workspace:Raycast(newTorsoPosition, 
			character.Torso.CFrame.LookVector*rayDistance, 
			raycastParams)
	if (raycastResult ~= nil) then
		print(raycastResult.Instance.Name)
		game.ReplicatedStorage.Remotes.MakePart
		--:FireServer(raycastResult.Position,newTorsoPosition)
		:FireServer(raycastResult,newTorsoPosition)
	end
end

Main Script (Inside the ServerScriptStorage)

---------------
-- Variables --
---------------

---------------
-- Functions --
---------------

local function CreateRay(raycastResult,newCharacterTorsoPos)
	print(raycastPosition.Instance)
	local distance = (newCharacterTorsoPos - raycastPosition.Position)
	.magnitude
	local part = Instance.new("Part")
	part.Transparency = 0.5
	part.CanCollide = false
	part.Anchored = true
	part.Material = Enum.Material.Neon
	part.BrickColor = BrickColor.new("Neon orange")
	part.Size = Vector3.new(0.3,0.3,distance)
	part.CFrame = CFrame.new(newCharacterTorsoPos,raycastPosition.Position) * CFrame.new(0,0,-distance / 2)
	part.Parent = game.Workspace.Rays
	wait(0.05)
	part:Destroy()
end

------------
-- Events --
------------

game.ReplicatedStorage.Remotes:WaitForChild("MakePart").OnServerEvent:Connect(function(plr, raycastResult,characterTorsoPos)
	CreateRay(raycastResult,characterTorsoPos)
end)

I pasted the code. I am checking if the code is nil, so that’s definitely not the case. Any more ideas?