Issues when ray casting to my target

I am getting lost here. I am trying to rework my system so it is not as laggy, right now, all I am doing is trying to make the rayCast on the client, but I seem to keep getting errors with the Target, would love some help, I can show you my current error, but every time I fix one thing it seems to break another.

Error 1: Attempt to connect failed: Passed value is not a function, Error 2: attempt to call a nil value

Most of my client sided code:

local function castRay(rTarget)

	--print("Target in castRay is set to"..rTarget)

	local laser_Thickness = .5

	local rayOriginPart = Eye
	local rayTargetPart = rTarget

	local origin = rayOriginPart.CFrame.Position
	local rayTarget = rayTargetPart.HumanoidRootPart.CFrame.Position
	local direction = (rayTarget - origin)

	local results = workspace:Raycast(origin, direction, RayCastParameters)

	if not results then
		results = {Position = rayTarget}

	local distance = (results.Position - origin).Magnitude

	LaserPart.Size =,laser_Thickness,distance)

	LaserPart.CFrame =, rayTarget) *,0, -distance / 2)

	--game.ReplicatedStorage.EyeEvent:FireAllClients(origin, rayTarget, distance, LaserPart)

local target

	target = Target

Main part of server script:

function startGame()
	local Target = FindNearest()
	local TargetPrimaryPart = Target or Target.PrimaryPart
	local Detector ="Part")
	Detector.Anchored = true
	Detector.CanCollide = false
	Detector.Transparency = 0.5
	Detector.Color = Color3.fromRGB(255, 0, 0)
	Detector.Size =, 1, 1) * 4

I can provide more information if needed.

For the first error:


Connect() requires a function. You’ve put () after the “castRay” function, which causes it to run the function and return its result. Since the function doesn’t return anything, it returns nil. In the end your line of code looks like this:


You should replace “castRay(target)” with “castRay”, but since you need to run the castRay function with an argument, you should do this instead:


The 2nd error might also be due to the same problem as above.