Raycasting Position "index nil w/ position"

Hi so i’m trying to raycast and in the serverscript when I’m grabbing the position of the raycast it’s returning nil and not working. “attempt to index nil with ‘Position’” in the error log.
Defining the Distance variable is the line where I grab the position and the error occurs there.

I’ve also seen this is likely occurring because it’s not hitting a part but it should be hitting parts. Is there something wrong with my origin and direction? It should be the players right hand and the direction is Mouse.Hit.Position.

--Local Script--
local inputService = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer
local Char = Player.CharacterAdded:Wait()
local Mouse = Player:GetMouse()
local Remote = script:WaitForChild("RemoteEvent")

local Pressed = false
local Enabled = true
local Key = nil

inputService.InputBegan:Connect(function(input, isTyping)
	if isTyping then return end
	if input.KeyCode == Enum.KeyCode.Q and Enabled == true then
		Enabled = false
		Pressed = true
		Remote:FireServer("Beam")
		Key = "Q"
	end
end)

inputService.InputEnded:Connect(function(input, isTyping)
	if isTyping then return end
	if input.KeyCode == Enum.KeyCode.Q and Key == "Q" then
		Pressed = false
		Key = nil
		script.RemoteEvent:FireServer("Release", Char.RightHand.Position, Mouse.Hit.Position)
		wait(5)
		Enabled = true
	end
end)
--Server Script--
local Player = game:GetService("Players")
local StarterPack = game:GetService("StarterPack")
local Remote = script.Parent
local debounce = false
local buttonDown = true
local RightHand1 = nil
local MousePos1 = nil

Remote.OnServerEvent:Connect(function(player,action,RightHand,MousePos)
	if action == "Release" then
		buttonDown = false
		RightHand1 = RightHand
		MousePos1 = MousePos
	end
if action == "Ray" and debounce == false then
		buttonDown = true
		local char = player.Character
		while buttonDown == true do
			wait()
		end
		local rayParams = RaycastParams.new()
		rayParams.FilterType = Enum.RaycastFilterType.Blacklist
		rayParams.FilterDescendantsInstances = {workspace.Game, char}
		rayParams.IgnoreWater = false
		local hit = workspace:Raycast(RightHand1, MousePos1, rayParams)
		
		local Distance = (RightHand1-hit.Position).magnitude
	end
end)

here’s a serverscript generated by openai

local Player = game:GetService("Players")
local StarterPack = game:GetService("StarterPack")
local Remote = script.Parent
local debounce = false
local buttonDown = true
local RightHand1 = nil
local MousePos1 = nil

Remote.OnServerEvent:Connect(function(player, action, RightHand, MousePos)
	if action == "Release" then
		buttonDown = false
		RightHand1 = RightHand
		MousePos1 = MousePos
	end
	
	if action == "Ray" and debounce == false then
		buttonDown = true
		local char = player.Character
		while buttonDown == true do
			wait()
		end
		local rayParams = RaycastParams.new()
		rayParams.FilterType = Enum.RaycastFilterType.Blacklist
		rayParams.FilterDescendantsInstances = {workspace.Game, char}
		rayParams.IgnoreWater = false
		local hit = workspace:Raycast(RightHand1, MousePos1, rayParams)
		
		if hit then
			local Distance = (RightHand1 - hit.Position).magnitude
		else
			print("Raycast did not hit a part")
		end
	end
end)

This script checks whether the raycast actually hit a part before attempting to access the Position property of the hit object. If the raycast did not hit a part, the script will print a message to the console indicating this.

I also moved the declaration of the Distance variable inside the if hit block, so that it is only defined if the raycast actually hits a part.

First of all, we want to avoid a situation in which you can possibly yield an error in the event the raycast fires into the skybox and thus returns a nil value. We can prevent this by simply checking if hit exists before continuing on with the code.

local hit = workspace:Raycast(RightHand1, MousePos1, rayParams)
if hit then
-- code
end

If you run print(hit.Instance) after using workspace:Raycast(), you will receive an output of the instance which the raycast hits. This way you can debug whether the raycast is hitting the expected target.

I also noticed that you are supplying the direction parameter of the raycast with a Position. If your intention is to fire a raycast towards the position of MousePos1, this will not work unless you manually calculate the direction from the RightHand1 to MousePos1 with the following equation: (RightHand1.Position - MousePos1).magnitude.

Hello,
So the ray cast is still returning nil and doesn’t work via the if statement. It’s not printing the instance it hits; It shows the same message just with Instance in this case (attempt to index nil with ‘Instance’). I also tried changing the direction parameter from MousePos1 to (RightHand1.Position - MousePos1).Magnitude and got Unable to cast double to Vector3 in the output.

means RightHand1 is nil. the error says Attempt to index nil with Position so logically you’re attempting to index RightHand1 with Position and unfortunately RightHand1 is nil and you get the error.

When I print(RightHand1) I get this though (-0.34569796919822693, 13.418097496032715, -3.413884401321411)

then dont use RightHand1.Position instead just RightHand1
@SoulStreets when the index nil with position error occured which line was causing it

Yeah I am using that lol, I tried RightHand1.Position but it doesn’t work because the variable is already meant for char.RightHand.Position

This line
local Distance = (RightHand1-hit.Position).magnitude
attempt to index nil with ‘Position’

then hit is nil


I know that hit is nil, hit is the raycast and I said it was nil in the initial post aswell as the first post you replied to. I do not understand why it is nil which is why I asked if something is wrong with my origin and direction. I’m aiming at a very large block directly in front of me, it should not be going to the skybox (if that’s what it’s doing).

try changing your server script to

--Server Script--
local Player = game:GetService("Players")
local StarterPack = game:GetService("StarterPack")
local Remote = script.Parent
local debounce = false
local buttonDown = true
local RightHand1 = nil
local MousePos1 = nil

Remote.OnServerEvent:Connect(function(player,action,RightHand,MousePos)
	if action == "Release" then
		buttonDown = false
		RightHand1 = RightHand
		MousePos1 = MousePos
	end
if action == "Ray" and debounce == false then
		buttonDown = true
		local char = player.Character
		while buttonDown == true do
			wait()
		end
		local rayParams = RaycastParams.new()
		rayParams.FilterType = Enum.RaycastFilterType.Blacklist
		rayParams.FilterDescendantsInstances = {workspace.Game, char}
		rayParams.IgnoreWater = false
		local hit = workspace:Raycast(RightHand1, MousePos1 + (CFrame.new(RightHand1,MousePos1).LookVector*3), rayParams)
		
		local Distance = (RightHand1-hit.Position).magnitude
	end
end)

@SoulStreets did it work

The direction is not the position you want it to end up in, it’s the origins position+the direction
So if you set the direction to Vector3.new(1,0,0) it would send a ray 1 stud long in this direction
image

It didn’t but I appreciate your help.
I ended up creating a function with the raycasting parameters as well as returning the position, that goes before the OnServerEvent. @damianz12 mentioned an if statement above.

local function castingRay() 
	local rayParams = RaycastParams.new()
	rayParams.FilterType = Enum.RaycastFilterType.Blacklist
	rayParams.FilterDescendantsInstances = {workspace.Game, char}
	rayParams.IgnoreWater = false
	local hit = workspace:Raycast(RightHand1, MousePos1, rayParams)
	if hit then
		return hit.Position
		else return nil
	end
end
		local hit = castingRay(RightHand1,MousePos1, char)
		if not hit or hit and (RightHand1-hit).magnitude > 200 then
			hit = CFrame.new(RightHand1,MousePos1)*CFrame.new(0,0,-200).Position
		end
		local Distance = (RightHand1-hit).magnitude

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