I am trying to raycast from the player to detect what part they are looking at. The issue is it keeps returning nil even if I am looking at a part. However, when I am right up close to a part it does return the raycast result as expected.
local player = script:FindFirstAncestorWhichIsA("Player")
local lookDir = game.ReplicatedStorage.remoteEvents.getCameraViewVector:InvokeClient(player)
local ray = workspace:Raycast(player.Character.Head.Position,
lookDir,
RaycastParams.new(
{
player.Character.Head,
player.Character.Torso,
player.Character.HumanoidRootPart,
},
Enum.RaycastFilterType.Blacklist,
false,
0
)
)
print(ray)
if ray.Instance ~= nil then
if ray.Instance.Parent:GetAttribute("isVendingMachine") then
print("you have bought a",ray.Instance.Parent:GetAttribute("gives"))
end
end
here’s the script that gets the view direction in case that’s the issue:
local remoteFunction = game.ReplicatedStorage.remoteEvents.getCameraViewVector
remoteFunction.OnClientInvoke = function()
local vector = game.Workspace.CurrentCamera.CFrame.LookVector
return vector
end