Why is it not adding a highlight to the tree?

i tried using just distance i also tried raycast but neither seem to work

local Players = game:GetService("Players")
local CollectionService = game:GetService("CollectionService")
local Trees = CollectionService:GetTagged("Tree")
local PlayerHumanoidRootPart = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")

local highlights = {}

for _, v in pairs(Trees) do
	if v:IsA("Model") then
		local treePos = v:GetPivot().Position
		local maxDistance =  10
		local rayDirection = (treePos - PlayerHumanoidRootPart.Position).Unit * maxDistance

		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
		raycastParams.FilterType = Enum.RaycastFilterType.Exclude
		raycastParams.IgnoreWater = true

		local raycastResult = workspace:Raycast(PlayerHumanoidRootPart.Position, rayDirection, raycastParams)

		if raycastResult and raycastResult.Instance == v then
			-- Tree is within the maxDistance
			if not highlights[v] then
				local newHighlight = game.ReplicatedStorage.Visuals:WaitForChild("Highlight"):Clone()
				newHighlight.Adornee = v:FindFirstAncestorOfClass("Model")
				newHighlight.Parent = Players.LocalPlayer:WaitForChild("PlayerGui")
				highlights[v] = newHighlight
			end
		else
			
			local highlight = highlights[v]
			if highlight then
				highlights[v] = nil
				highlight:Destroy()
			end
		end
	end
end

You should try prints to see if its the code, but it may also be because roblox has a limit on highlights. Only a certian number of highlights (I think 31) can be rendered at once.

1 Like