workspace:Raycast only return a RaycastResult when it gets intersected by a valid object.
Therefore, it will only exist if the ray hits something.
Here’s an example bit of code:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local camera = workspace.Camera
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = {character}
params.IgnoreWater = false
params.RespectCanCollide = true
local raycast = workspace:Raycast(camera.CFrame.Position, camera.CFrame.LookVector * 10, params) --// This will only be equal to something if the raycast hits something
if raycast then
print("Raycast hit something:", raycast --[[prints all of the data in the RaycastResult (i.e. Instance, Position, etc.)]])
if raycast.Instance:IsA("BasePart") then
raycast.Instance.BrickColor = BrickColor.new("Really red")
end
else
print("Raycast did not hit an object")
end