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
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)
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
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.
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
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)