Hide all players with radius 30 studs

Im wanna to do like when player is close to other player they will be able to see each other.
I also tried to do this but its didnt work
Heres script that i choosed and it didnt work:

local plrs = {}
local Player = game.Players.LocalPlayer
local hiding = false
game["Run Service"].RenderStepped:Connect(function()
		local player = game.Players:GetChildren()
	for i = 1,#player do -- hide
			if player[i] ~= Player then
			local magni = (Player.Character.Torso.Position - player[i].Character.Torso.Position).magnitude
			local range = 15
			if magni < range then
				table.insert(plrs,workspace[player[i].Name])
else -- show
player[i].Parent = nil
			end
		end
end
end)
2 Likes

I can give my module, it gives you a table of players in the radius you set.

can i have it then? im gonna edit it a little bit

1 Like

give me a seс, ill write your a code

Do you want to hide all players more then 30 studs away from the current player?
Or all players closer than 30 studs from the current player?

ty for waiting
here:
detect.rbxm (4.8 KB)

module to RepStorage
LocalScript to character

2 Likes

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
3 Likes

This will make it so that, any character more than 30 studs away becomes invisible
When close to another player

A bit further, the player has started to dissapear

More than 30 studs away, the player has dissapeared from our screen entirely

If you want the transparency to be immediate, set min Distance to 30, and max to 1

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.