Why this Raycast script isn't working?

Hi Guys, so I made a simple raycast script like this -

local player = game.Players.LocalPlayer

player.CharacterAdded:Connect(function(character)
local rayParams = RaycastParams.new():AddToFilter({character.Head})

game:GetService("RunService").RenderStepped:Connect(function()
	local ray = workspace:Raycast(character.Head.Position, character.Head.CFrame.LookVector * 5, rayParams)
		print(ray.Instance.Name)
	if ray.Instance and ray.Instance.Name == "Tree" and ray.Instance ~= character.Head then
		local highlight = Instance.new("Highlight", ray.Instance)
		highlight.FillTransparency = 1
		highlight.OutlineColor = Color3.new(0.0862745, 0.0862745, 0.0862745)
	end
	
  end)
end)

But this doesn’t work for some reason. Any help is appreciated, thanks :slight_smile:

2 Likes

Do you get any errors in the console?

I suspect it could be because the Character could already be added when this script runs, so CharacterAdded might not fire or the raycast could be intersecting the accessories or other parts on your character that isnt the Head.

-- VARIABLES
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait() -- Gets the character, if there is no character we will wait for it to be added.
local rayParams = RaycastParams.new()
rayParams:AddToFilter({character}) -- EDITED: Changed character.Head to character


-- EVENTS
game:GetService("RunService").RenderStepped:Connect(function()
	local ray = workspace:Raycast(character.Head.Position, character.Head.CFrame.LookVector * 5, rayParams)
	if ray.Instance and ray.Instance.Name == "Tree" and ray.Instance ~= character.Head then
		local highlight = Instance.new("Highlight", ray.Instance)
		highlight.FillTransparency = 1
		highlight.OutlineColor = Color3.new(0.0862745, 0.0862745, 0.0862745)
	end
end)
2 Likes

I tried the code you sent, it errors that I’m trying to index nil with “Instance”, which means the character isn’t loaded maybe.

If you want more info, I have setted the CameraMode to LockFirstPerson.

2 Likes

Okay yeah, small error there, I just noticed too.

This should work here:

if ray and ray.Instance.Name == "Tree" then -- Check if there is a Raycast result returned, then check if the part we intersected is named Tree
		local highlight = Instance.new("Highlight", ray.Instance)
		highlight.FillTransparency = 1
		highlight.OutlineColor = Color3.new(0.0862745, 0.0862745, 0.0862745)
end
2 Likes

Ok, so it doesn’t error anymore but for some reason, it doesn’t add the Highlight object everytime, it just adds it for the first tree I look and then stops.

3 Likes

I think its because Roblox has a limit on how many highlights objects can have (its around 25 or 35, something around that I forgot!)

The code is just putting a bunch of highlights in to the same tree part if we’re staring at it, which causes you to reach the Highlight cap roblox put in place.

if ray and ray.Instance.Name == "Tree" and not ray.Instance:FindFirstChild("TreeHL") then -- Check if there is a Raycast result returned, then check if the part we intersected is named Tree & checks if the tree doesn't have a Highlight in it.
   	local highlight = Instance.new("Highlight", ray.Instance)
   	highlight.Name = "TreeHL"
   	highlight.FillTransparency = 1
   	highlight.OutlineColor = Color3.new(0.0862745, 0.0862745, 0.0862745)
end

Keep in mind you’ll have to clean up some highlights to avoid reaching the max cap

2 Likes

If you don’t mind, can I ask one more question?

How can we remove the highlight object from the tree after we are not looking at it?

1 Like

One solution for this I can think of is creating a Highlight and using the Adornee property (which just selects what Instance the Highlight is highlighting)

-- VARIABLES
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait() -- Gets the character, if there is no character we will wait for it to be added.
local rayParams = RaycastParams.new()
rayParams:AddToFilter({character}) -- EDITED: Changed character.Head to character

local highlight = Instance.new("Highlight") -- We will use this highlight for every tree Instance we stare at.
highlight.Enabled = false -- We will only have this enabled when looking at trees.
highlight.Name = "TreeHL"
highlight.FillTransparency = 1
highlight.OutlineColor = Color3.new(0.0862745, 0.0862745, 0.0862745)
highlight.Parent = script

-- EVENTS
game:GetService("RunService").RenderStepped:Connect(function()
	local ray = workspace:Raycast(character.Head.Position, character.Head.CFrame.LookVector * 5, rayParams)
	if ray and ray.Instance.Name == "Tree" then -- Check if there is a Raycast result returned, then check if the part we intersected is named Tree & checks if the tree doesn't have a Highlight in it.
		highlight.Adornee = ray.Instance -- Selects the tree Instance to Highlight
		highlight.Enabled = true -- Enables the highlight
	else
        -- This just disables highlight effect if we're not looking at a tree
		highlight.Enabled = false
		highlight.Adornee = nil
	end
end)
1 Like

This works perfectly! Thanks alot bro, you don’t know how much you helped me! :smiley:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.