Need help with raycasting

So i got a mining game, And i want to make it so that mobile players mine from the middle of their screen. But i get this type of error when i try to test this on mobile: attempt to index nil with 'Position'
Broken piece of code:

	local RaycastResult = workspace:Raycast(cam.CFrame.Position, cam.CFrame.LookVector*math.huge, RaycastParams.new())
	local increment = 6
	local currentPosition = RaycastResult.Position
	local roundedPosition = Vector3.new(
		math.floor((currentPosition.X + 3) / increment) * increment,
		math.floor((currentPosition.Y + 3) / increment) * increment,
		math.floor((currentPosition.Z + 3) / increment) * increment
	)

	local selectedObject = nil

	if Device:getPlatform() == "Mobile" then
		for _, v in pairs(workspace:GetDescendants()) do
			if v:IsA("BasePart") and v.Position == roundedPosition then
				selectedObject = v
			end
		end
	else
		selectedObject = Mouse.Target
	end
3 Likes

On what line is the error occurring?

2 Likes

Line 28 (character limit i hate this)

2 Likes

and which line is that on the code you sent?

2 Likes

local currentPosition = RaycastResult.Position

2 Likes

Multiplying the LookVector with math.huge is probably breaking it, try changing
workspace:Raycast(cam.CFrame.Position, cam.CFrame.LookVector*math.huge, RaycastParams.new())
to
workspace:Raycast(cam.CFrame.Position, cam.CFrame.LookVector)

2 Likes

That did not fix the error. (char limit)

2 Likes

Before getting the raycast’s position, check if RaycastResult isn’t null and RaycastResult.Instance also isn’t null.

2 Likes

So this fixed that issue, Now i am getting this issue:
attempt to index nil with X
which would indicate currentPosition.X in the roundedPosition local

2 Likes

The error is occuring because RaycastResult could be nil. Try this:

local currentPosition

if RaycastResult then
     currentPosition = RaycastResult.Position
end
2 Likes

Instead of looping through the workspace and checking if the position of v is the same as roundedPosition, just set the selectedObject to RaycastResult.Instance which is the part that was hit by the raycast
So the code would be,

local selectedObject = nil

if Device:getPlatform() == "Mobile" then
	local RaycastResult = workspace:Raycast(cam.CFrame.Position, cam.CFrame.LookVector)
	
	if RaycastResult and RaycastResult.Instance then
		selectedObject = RaycastResult.Instance
	end
else
	selectedObject = Mouse.Target
end
3 Likes

I just added a print(selectedObject), and it constantly prints nil when i try to test it on mobile.
Current code:

	local RaycastResult = workspace:Raycast(cam.CFrame.Position, cam.CFrame.LookVector, RaycastParams.new())
	--[[local currentPosition
	local increment = 6
	if RaycastResult~=nil and RaycastResult.Instance~=nil then
		currentPosition=RaycastResult.Position
	end
	local roundedPosition = Vector3.new(
		math.floor((currentPosition.X + 3) / increment) * increment,
		math.floor((currentPosition.Y + 3) / increment) * increment,
		math.floor((currentPosition.Z + 3) / increment) * increment
	)]]

	local selectedObject = nil

	if Device:getPlatform() == "Mobile" then
		local RaycastResult = workspace:Raycast(cam.CFrame.Position, cam.CFrame.LookVector)

		if RaycastResult and RaycastResult.Instance then
			selectedObject = RaycastResult.Instance
		end
	else
		selectedObject = Mouse.Target
	end
	print(selectedObject)
	Selected.Value = selectedObject
1 Like

bump
(character limit, i hate this thing)

workspace:Raycast() is breaking because you are multiplying the lookvector by math.huge

You should define a raycast distance outside of the main function and use that instead of math.huge

local RaycastResult = workspace:Raycast(cam.CFrame.Position, cam.CFrame.LookVector*math.huge, RaycastParams.new())

→

local Distance = 100
workspace:Raycast(cam.CFrame.Position, cam.CFrame.LookVector * Distance, RaycastParams.new()

So this fixed it, But now i want to make it so that my params ignore player’s characters entirely so that they can mine straight down and while zoomed out
Current code:

	local selectedObject = nil
	local params = RaycastParams.new()
	local instances = {}
	params.IgnoreWater=true
	params.FilterType=Enum.RaycastFilterType.Exclude
	params.FilterDescendantsInstances=instances
	for _,v in pairs(game.Players:GetPlayers()) do
		for _,i in pairs(v.Character:GetChildren()) do
			table.insert(instances, i)
		end
	end
	local RaycastResult = workspace:Raycast(cam.CFrame.Position, cam.CFrame.LookVector*100, params)
	if Device:getPlatform() == "Mobile" then

		if RaycastResult and RaycastResult.Instance then
			selectedObject = RaycastResult.Instance
		end
	else
		selectedObject = Mouse.Target
	end
	Selected.Value = selectedObject

you can just insert the entire character instead of iterating through everything

	for _, P in game.Players:GetPlayers() do
		if not P.Character then continue end
		table.insert(instances, P.Character)
	end

the code seems fine as-is and i dont see why it’s not ignoring the characters

I also suggest starting the raycast from the character’s rootpart and not the camera

reason why i want it to ignore characters is because if i zoom out and i still want to mine, my character blocks the raycast

	params.FilterDescendantsInstances=instances

try adding this after the loop

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