Untitled

 avatar
unknown
plain_text
3 months ago
2.5 kB
6
Indexable
local renddistance = 100
local interval = 0.5
local maxPlayersInRadius = 10
local cullingPercentage = 0.4

local character = script.Parent
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")

local function updateVisibility()
	local playersInRadius = {}

	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		if player.Character and player.Character ~= character then
			local otherRoot = player.Character:FindFirstChild("HumanoidRootPart")
			if otherRoot then
				local distance = (humanoidRootPart.Position - otherRoot.Position).Magnitude
				if distance <= renddistance then
					table.insert(playersInRadius, {player = player, distance = distance})
				end
			end
		end
	end

	if #playersInRadius > maxPlayersInRadius then
		table.sort(playersInRadius, function(a, b)
			return a.distance < b.distance
		end)

		local playersToHide = math.ceil(#playersInRadius * cullingPercentage)

		for i = maxPlayersInRadius + 1, #playersInRadius do
			local player = playersInRadius[i].player
			if player.Character then
				for _, part in pairs(player.Character:GetDescendants()) do
					if part:IsA("BasePart") then
						part.LocalTransparencyModifier = 1
					end
				end
			end
		end

		for i = 1, math.min(maxPlayersInRadius, #playersInRadius) do
			local player = playersInRadius[i].player
			if player.Character then
				for _, part in pairs(player.Character:GetDescendants()) do
					if part:IsA("BasePart") then
						part.LocalTransparencyModifier = 0
					end
				end
			end
		end
	else
		for _, playerData in pairs(playersInRadius) do
			local player = playerData.player
			if player.Character then
				for _, part in pairs(player.Character:GetDescendants()) do
					if part:IsA("BasePart") then
						part.LocalTransparencyModifier = 0
					end
				end
			end
		end
	end

	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		if player.Character and player.Character ~= character then
			local otherRoot = player.Character:FindFirstChild("HumanoidRootPart")
			if otherRoot then
				local distance = (humanoidRootPart.Position - otherRoot.Position).Magnitude
				if distance > renddistance then
					for _, part in pairs(player.Character:GetDescendants()) do
						if part:IsA("BasePart") then
							part.LocalTransparencyModifier = 1
						end
					end
				end
			end
		end
	end
end

while true do
	wait(interval)
	updateVisibility()
end
Editor is loading...
Leave a Comment