Issues with ScreenPointToRay

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Get the color of the hit part and set the Frame BackgroundColor to hit.Color
  2. What is the issue? Include screenshots/videos if possible!

ScreenPointToRay doesn’t seem to work? No hit event occurs.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Honestly, not sure if I know about any solutions myself. Yes I did look, haven’t found much.

I’m creating a simple script that will ‘render’ the camera view.
Encountered an issue that makes my further work impossible to complete, I have no more ideas on how to approach that problem.


local Service = {}

Service.RunService = game:GetService("RunService")
Service.Players    = game:GetService("Players")

local Player = Service.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera

local Viewport = Camera.ViewportSize

repeat wait()
    Camera.CameraType = Enum.CameraType.Scriptable until
    Camera.CameraType == Enum.CameraType.Scriptable
Camera.CFrame = workspace.CFrame.CFrame

Character = Character:GetChildren()
wait(2)
for x = 1,100 do
    wait()
    for y = 1,100 do
        local Frame = Instance.new("Frame",script.Parent)
        Frame.BorderSizePixel = 0
        Frame.BackgroundColor3 = Color3.fromRGB(0,0,0)
        Frame.Size = UDim2.new(0,10,0,10)
        Frame.Position = UDim2.new(0,x*10,0,y*10)
        
        local unitRay = Camera:ScreenPointToRay(x, y)
        local ray = Ray.new(unitRay.Origin, unitRay.Direction * 1000)
        local hit,pos,norm = workspace:FindPartOnRayWithWhitelist(ray,Character)
        if (hit) then
            print(hit.Name)
            Frame.BackgroundColor3 = hit.Color
        else
            Frame.BackgroundColor3 = Color3.fromRGB(14, 171, 255)
        end
    end
end 

Any help will be truly appreciated.

The issue is your using workspace:FindPartOnRayWithWhitelist, this will only return an object intersecting with the ray that is contained in the white list, I think you meant to use FindPartOnRayWithIgnoreList, you would do this:

--change this line: 
local hit,pos,norm = workspace:FindPartOnRayWithWhitelist(ray,Character)
--to this
local hit,pos,norm = FindPartOnRayWithIgnoreList(ray,{Character})
2 Likes

Oh my god. How did i not notice that.
Thanks a lot for pointing it out.
Solved,

2 Likes