Hey devs. I have a problem. I’m not very good at raycasting, and I really need some help with this script. Basically, the goal is to go through every Instance
in the workspace and see if it’s close enough to change its color to red. However, like I said before, I’m not great at raycasting, and this is my awful script down here that nearly crashed the game.
function CastRay(Caster)
local Descendants = workspace:GetDescendants()
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Include
raycastParams.FilterDescendantsInstances = {workspace.Model}
raycastParams.IgnoreWater = true
for _, v in ipairs(Descendants) do
local raycastResult = workspace:Raycast(Caster.Position, Caster.CFrame.LookVector * 50, raycastParams)
if raycastResult then
local Inst = raycastResult.Instance
if Inst and Inst:IsA("Part") then
print("Object hit by ray:", raycastResult.Instance:GetFullName())
print("Position:", raycastResult.Position)
Inst.Color = Color3.fromRGB(200, 0, 0)
print("The part's color is now:", Inst.Color)
end
else
print("Nothing was hit!")
end
end
end
local Caster = script.Parent.Caster
while true do
task.wait(3)
CastRay(Caster)
end