Make raycast fire from center of camera

So currently in my game i have an ability that when you press q, it fires the server and runs this script. However, instead of raycasting from the humanoid root part, i want it so it fires where a crosshair is (always center of the screen). Whenever i attempt to fire information with the remote event of the current cameras c frame, the server says that its nil
Below is my normal, raycast in front of player server script and part of local script. Does anyone have an idea of how to make it fire from center of camera. Ive read through forums but nothing works

local crystalspear = game.ServerStorage.CrystalSpear:Clone()
	--local particles = game.ServerStorage.CrystalSpear.ParticleEmitter:Clone()
	crystalspear.Parent = workspace
	--particles.Parent = crystalspear
	game.Debris:AddItem(crystalspear, 9)
	crystalspear.Position = character.Character.Default.BodyAttach.Position
	local RAY_RANGE = 40
	local ignore = {character.Character, character.Character.Default:GetChildren()}
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = ignore
	local cframe = character.Character:GetPivot()
	local result = workspace:Raycast(cframe.Position, cframe.LookVector * RAY_RANGE, params)
	local goal = {}
	local once = false
	if result then
		crystalspear.CFrame = CFrame.lookAt(crystalspear.Position, result.Position) * CFrame.Angles(0,math.rad(90),0)
		local position = result.Position
		print(position)
		print(character.Character.HumanoidRootPart.Position)
		goal.Position = position
	else
		goal.Position = character.Character.HumanoidRootPart.Position + character.Character.HumanoidRootPart.CFrame.LookVector * RAY_RANGE
		crystalspear.CFrame = CFrame.lookAt(crystalspear.Position, goal.Position) * CFrame.Angles(0,math.rad(90),0)

-- local script below
if input.KeyCode == Enum.KeyCode.E then
		print("ive been pressed")
		character.Ability1:FireServer(character, "E")
	elseif input.KeyCode == Enum.KeyCode.Q then
		character.Ability2:FireServer(character, "Q")
	end
1 Like

This is likely because the camera’s CFrame is being accessed on the client side, but it is not correctly transmitted or handled on the server side. The server does not have direct access to the client’s camera, which is why the CFrame would be nil when trying to use it on the server. Hope this helps!

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