Assuming you want to hide all players more than 30 studs away, I wrote a script for that
Place this inside StarterPlayer → StarterPlayerScripts, it’s a LocalScript
Now, setting other player’s character to nil is a really bad idea for a multitide of reasons, using transparency is better
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Player :Player = Players.LocalPlayer
local transparencyCache = {}
local maxVisualDistance = 30 --The distance at which character becomes fully transparent
local minVisualDistance = 20 --The distance after which transparency begins
local Properties = {
Transparency = 1,
TextTransparency = 1,
BackgroundTransparency = 1,
TextStrokeTransparency = 1,
ImageTransparency = 1,
Opacity = 0
}
function getTransparencyValue(currValue,finValue,alpha)
local function lerp(a, b, t)
return a * (1 - t) + b * t
end
if typeof(currValue) == "NumberRange" then
return NumberRange.new(lerp(currValue.Min,finValue,alpha),lerp(currValue.Max,finValue,alpha))
elseif typeof(currValue) == "NumberSequence" then
local Keypoints = currValue.Keypoints
for i,v in pairs(Keypoints) do
Keypoints[i] = NumberSequenceKeypoint.new(v.Time,lerp(v.Value,finValue,alpha),v.Envelope)
end
return NumberSequence.new(Keypoints)
elseif typeof(currValue) == "number" then
return lerp(currValue,finValue,alpha)
else
return currValue
end
end
function Main()
local Position = (Player.Character and Player.Character:FindFirstChild("HumanoidRootPart") and Player.Character.HumanoidRootPart.Position) or workspace.CurrentCamera.CFrame.Position -- Get our player's position, if the character isn't there, use the camera's position
for _,v in ipairs(Players:GetPlayers()) do
if v == Player then continue end --Skip our player, continue with the rest
transparencyCache[v] = transparencyCache[v] or {}
local Character = v.Character
if Character then
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart") or Character:FindFirstChild("UpperTorso") or Character:FindFirstChild("Torso") --Find either the HumanoidRootPart or the UpperTorso/Torso so we can check if the player is close
if HumanoidRootPart then
for ii,vv in ipairs(Character:GetDescendants()) do --Loop through and transparencify the objects inside the character depending on distance
transparencyCache[v][vv] = transparencyCache[v][vv] or {}
for iii,vvv in pairs(Properties) do
if pcall(function() return vv[iii] end) then
local Distance = (Position - HumanoidRootPart.Position).Magnitude
transparencyCache[v][vv][iii] = transparencyCache[v][vv][iii] or vv[iii] --Store the original object's transparency so this can be returned
local alpha = math.clamp((Distance - minVisualDistance)/maxVisualDistance,0,1) --This is a value between 0 and 1, based on the min and max distances
vv[iii] = getTransparencyValue(transparencyCache[v][vv][iii],vvv,alpha) -- Adjust transparency according to the alpha
end
end
end
end
end
end
end
RunService:BindToRenderStep("playerVisualDistance",Enum.RenderPriority.Camera.Value,Main)
Players.PlayerRemoving:Connect(function(Player) --Immediately removes dead players from the table, no memory leaks
if transparencyCache[Player] then
for ii in pairs(transparencyCache[Player]) do
transparencyCache[Player][ii] = nil
end
transparencyCache[Player] = nil
end
end)
while task.wait(5) do --This will remove dead values from the table, no memory leaks
for i,v in pairs(transparencyCache) do
for ii in pairs(transparencyCache[i]) do
if not (i.Character and ii:IsDescendantOf(i.Character)) then
transparencyCache[i][ii] = nil
end
end
end
end