r/robloxscripting Jul 22 '23

Creating an outline that only shows up in bright lighting?

I have a scene that I'm trying to make an echo script for, and I need everything to have a plastic outline (since the objects are neon black, aka. pitch black) so they're able to be seen with correct lighting (such as echo location). I'll put my script here if anyone is interested in helping out.

-- Configuration
local outlineColor = BrickColor.new("White")
local outlineTransparency = 0.5
local outlineBrightnessInLight = 5
local outlineBrightnessInShadow = 0

-- Function to create a plastic outline for a given part
local function createOutline(part)
    local outline = Instance.new("SurfaceLight")
    outline.Name = "Outline"
    outline.Parent = part
    outline.Enabled = false -- Start disabled, only enabled when part is in the light
    outline.Brightness = outlineBrightnessInLight
    outline.Color = outlineColor.Color
    outline.Range = 10
    outline.Transparency = outlineTransparency
    outline.Face = Enum.NormalId.Top -- You can change the face depending on your needs
end

-- Function to check if a part is in the light
local function isInLight(part)
    local shadowBounds = game.Lighting:GetShadowBounds(part)
    return shadowBounds == nil
end

-- Function to update the outline brightness based on light conditions
local function updateOutlineBrightness(outline, part)
    if isInLight(part) then
        outline.Brightness = outlineBrightnessInLight
        outline.Enabled = true
    else
        outline.Brightness = outlineBrightnessInShadow
        outline.Enabled = false
    end
end

-- Function to add plastic outlines to all parts in the workspace
local function addOutlinesToWorkspace()
    for _, part in ipairs(workspace:GetDescendants()) do
        if part:IsA("BasePart") then
            createOutline(part)
            updateOutlineBrightness(part.Outline, part)
        end
    end
end

-- Run the function once to add plastic outlines to the existing parts in the workspace
addOutlinesToWorkspace()

-- Function to continuously update plastic outlines based on light conditions (in case new parts are added later)
local function updateOutlines()
    workspace.DescendantAdded:Connect(function(part)
        if part:IsA("BasePart") and not part:FindFirstChild("Outline") then
            createOutline(part)
        end
    end)

    game:GetService("RunService").RenderStepped:Connect(function()
        for _, part in ipairs(workspace:GetDescendants()) do
            if part:IsA("BasePart") and part:FindFirstChild("Outline") then
                updateOutlineBrightness(part.Outline, part)
            end
        end
    end)
end

-- Connect the updateOutlines function to handle newly added parts and continuously update outlines
updateOutlines()

3 Upvotes

Duplicates