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
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)
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
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
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