Hello. I was trying to make a system where it would send a raycast from your camera, to your mouse. However, I keep getting this error, and I don’t know what is wrong.
attempt to index nil with 'Position'
Here is the localscript:
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local camera = workspace.CurrentCamera
local function spawnDummy()
local camPos = camera.CFrame.Position
local mousePos = mouse.Hit.Position
local raycast = workspace:Raycast(camPos,mousePos)
print(raycast.Position)
local dummy = game:GetService("ReplicatedStorage"):WaitForChild("Rig"):Clone()
dummy.HumanoidRootPart.Position = raycast.Position
dummy.Parent = workspace
end
mouse.Button1Down:Connect(spawnDummy)
i use this function in my game, idk if its what your looking for
local GetMouse3DPosition = function()
local unitRay = Camera:ScreenPointToRay(Mouse.X, Mouse.Y)
local raycastResult = workspace:Raycast(unitRay.Origin, unitRay.Direction * 10000)
if raycastResult then
return raycastResult.Position
else
return nil
end
end