How to make it the more you are close to an NPC the less transparent a frame will become

Hello, Don’t know how to make it the more closer you are to the npc the less transparent a frame will become.

Thanks.

--LOCAL

local Game = game
local Script = script
local Workspace = workspace
local RunService = Game:GetService("RunService")
local Players = Game:GetService("Players")
local Player = Players.LocalPlayer
local Frame = Script.Parent --Place this script inside the frame.
local NPC = Workspace:WaitForChild("NPC") --Reference to the NPC.

local function OnRenderStep()
	local Distance = Player:DistanceFromCharacter(NPC:GetPivot().Position)
	if Distance == 0 then return end
	Frame.Transparency = math.clamp(Distance / 50, 0, 1)
end

RunService.RenderStepped:Connect(OnRenderStep)

--At a distance of 50 studs or greater the frame is completely transparent.
--At a distance of 0 studs the frame is completely opaque.
1 Like