I want to highlight player’s outline through wall when ever they walk into a place where camera can’t see
I tried using raycast which is working but the outline just stay there even if it suppose to be de activated after player’s character is appear again
game:GetService("RunService").RenderStepped:Connect(function(deltaTime)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent}
local position = workspace.CurrentCamera.CFrame.Position
local direction = script.Parent.PrimaryPart.Position - position
local raycastResult = workspace:Raycast(position, direction, raycastParams)
local playerOutline = Instance.new("Highlight")
playerOutline.DepthMode = Enum.HighlightDepthMode.AlwaysOnTop
playerOutline.FillTransparency = 1
playerOutline.OutlineTransparency = 0
playerOutline.OutlineColor = Color3.fromRGB(255, 255, 255)
if raycastResult ~= nil then
playerOutline.Parent = script.Parent
else
playerOutline.Parent = nil
end
end)
You’re creating a new highlight every single frame the player is obscured. This is a massive memory leak and also the reason they’re not disappearing.
Try this:
local playerOutline = Instance.new("Highlight")
playerOutline.DepthMode = Enum.HighlightDepthMode.AlwaysOnTop
playerOutline.FillTransparency = 1
playerOutline.OutlineTransparency = 0
playerOutline.OutlineColor = Color3.fromRGB(255, 255, 255)
game:GetService("RunService").RenderStepped:Connect(function(deltaTime)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent}
local position = workspace.CurrentCamera.CFrame.Position
local direction = script.Parent.PrimaryPart.Position - position
local raycastResult = workspace:Raycast(position, direction, raycastParams)
if raycastResult ~= nil then
playerOutline.Parent = script.Parent
else
playerOutline.Parent = nil
end
end)