Загрузка данных


local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local basesFolder = workspace:WaitForChild("Bases")
local FULL_VISIBLE_DIST = 150
local INVISIBLE_DIST = 200
local ZONE_FADE_SPEED = 5
local function getBaseBillboardGuis()
	local guis = {}
	for _, base in ipairs(basesFolder:GetChildren()) do
		if base:IsA("Folder") then
			local nicknamePart = base:FindFirstChild("NicknameAndAvatarPlayer")
			if nicknamePart then
				local billboardGui = nicknamePart:FindFirstChildOfClass("BillboardGui")
				if billboardGui then
					local baseZone = nicknamePart:FindFirstChild("BaseZone")
					table.insert(guis, { part = nicknamePart, gui = billboardGui, baseZone = baseZone, zoneTransparency = 0 })
				end
			end
		end
	end
	return guis
end
local baseGuis = getBaseBillboardGuis()
local function updateOwnerVisibility()
	local myName = player.Name
	for _, data in ipairs(baseGuis) do
		local base = data.part.Parent
		if base then
			local values = base:FindFirstChild("Values")
			if values then
				local owner = values:FindFirstChild("Owner")
				if owner and owner:IsA("StringValue") then
					data.isOwner = (owner.Value == myName)
					data.gui.Enabled = owner.Value ~= ""
				end
			end
		end
	end
end
local function connectOwnerChanged()
	for _, base in ipairs(basesFolder:GetChildren()) do
		if base:IsA("Folder") then
			local values = base:FindFirstChild("Values")
			if values then
				local owner = values:FindFirstChild("Owner")
				if owner and owner:IsA("StringValue") and not owner:GetAttribute("_fadeConnected") then
					owner:SetAttribute("_fadeConnected", true)
					owner.Changed:Connect(function()
						updateOwnerVisibility()
					end)
				end
			end
		end
	end
end
connectOwnerChanged()
updateOwnerVisibility()
basesFolder.DescendantAdded:Connect(function(descendant)
	if descendant.Name == "Owner" and descendant:IsA("StringValue") then
		descendant.Changed:Connect(function()
			updateOwnerVisibility()
		end)
		updateOwnerVisibility()
	elseif descendant:IsA("BillboardGui") and descendant.Parent and descendant.Parent.Name == "NicknameAndAvatarPlayer" then
		local baseZone = descendant.Parent:FindFirstChild("BaseZone")
		table.insert(baseGuis, { part = descendant.Parent, gui = descendant, baseZone = baseZone, zoneTransparency = 0 })
		updateOwnerVisibility()
	end
end)
RunService.RenderStepped:Connect(function(dt)
	local cameraPos = camera.CFrame.Position
	local character = player.Character

	for _, data in ipairs(baseGuis) do
		local gui = data.gui
		if gui.Enabled then
			local dist = (data.part.Position - cameraPos).Magnitude
			local distTransparency
			if dist <= FULL_VISIBLE_DIST then
				distTransparency = 0
			elseif dist >= INVISIBLE_DIST then
				distTransparency = 1
			else
				distTransparency = (dist - FULL_VISIBLE_DIST) / (INVISIBLE_DIST - FULL_VISIBLE_DIST)
			end
			local targetZoneTransparency = 0
			if data.isOwner and data.baseZone and character then
				local hrp = character:FindFirstChild("HumanoidRootPart")
				if hrp then
					local charPos = hrp.Position
					local zoneCF = data.baseZone.CFrame
					local zoneSize = data.baseZone.Size
					local localPos = zoneCF:PointToObjectSpace(charPos)
					local halfSize = zoneSize / 2
					local inside = math.abs(localPos.X) <= halfSize.X
						and math.abs(localPos.Y) <= halfSize.Y
						and math.abs(localPos.Z) <= halfSize.Z
					if inside then
						targetZoneTransparency = 1
					end
				end
			end
			data.zoneTransparency = data.zoneTransparency + (targetZoneTransparency - data.zoneTransparency) * math.min(1, ZONE_FADE_SPEED * dt)
			local transparency = math.max(distTransparency, data.zoneTransparency)
			local textLabel = gui:FindFirstChildOfClass("TextLabel")
			if textLabel then
				textLabel.TextTransparency = transparency
				textLabel.TextStrokeTransparency = transparency
			end
			local imageLabel = gui:FindFirstChild("AvatarImage")
			if imageLabel then
				imageLabel.ImageTransparency = transparency
				local stroke = imageLabel:FindFirstChildOfClass("UIStroke")
				if stroke then
					stroke.Transparency = transparency
				end
			end
		end
	end
end)