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


local Players = game:GetService("Players")
local basesFolder = workspace:WaitForChild("Bases")
local function findFreeBase()
	for _, base in ipairs(basesFolder:GetChildren()) do
		if base:IsA("Folder") then
			local values = base:FindFirstChild("Values")
			if values then
				local ownerValue = values:FindFirstChild("Owner")
				if ownerValue and ownerValue:IsA("StringValue") and ownerValue.Value == "" then
					return base, ownerValue
				end
			end
		end
	end
	return nil, nil
end
local function getPlayerBase(player)
	local baseName = player:GetAttribute("AssignedBase")
	if baseName then
		return basesFolder:FindFirstChild(baseName)
	end
	return nil
end
local function assignBase(player, base, ownerValue)
	ownerValue.Value = player.Name
	player:SetAttribute("AssignedBase", base.Name)
	local places = base:FindFirstChild("Places")
	if places then
		local startplace = places:FindFirstChild("startplace")
		if startplace and startplace:IsA("BasePart") then
			startplace.Transparency = 0
			startplace.CastShadow = true
		end
	end
	local spawnLoc = base:FindFirstChild("BaseSpawnLocation")
	if spawnLoc and spawnLoc:IsA("SpawnLocation") then
		spawnLoc.Enabled = true
		player.RespawnLocation = spawnLoc
	end

	local oldBuildingData = base:FindFirstChild("BuildingData")
	if oldBuildingData then
		oldBuildingData:Destroy()
	end

	if _G.LoadPlayerData then
		task.wait(0.1)
		_G.LoadPlayerData(player)
	end
	player.CharacterAdded:Connect(function()
		onCharacterAdded(player)
	end)
	if player.Character then
		onCharacterAdded(player)
	end
end
local function onCharacterAdded(player)
	local character = player.Character
	if not character then return end

	local base = getPlayerBase(player)
	if base then
		local spawnPart = base:FindFirstChild("Spawn")
		if spawnPart and spawnPart:IsA("BasePart") then
			character:PivotTo(spawnPart.CFrame + Vector3.new(0, 3, 0))
		end
	end
end
Players.PlayerAdded:Connect(function(player)
	for _, base in ipairs(basesFolder:GetChildren()) do
		if base:IsA("Folder") then
			local values = base:FindFirstChild("Values")
			if values then
				local ownerValue = values:FindFirstChild("Owner")
				if ownerValue and ownerValue:IsA("StringValue") and ownerValue.Value == player.Name then
					ownerValue.Value = ""
				end
			end
		end
	end
	local base, ownerValue = findFreeBase()
	if base then
		assignBase(player, base, ownerValue)
	else
		warn("Не хватило базы игроку" .. player.Name)
	end
end)
Players.PlayerRemoving:Connect(function(player)
	local baseName = player:GetAttribute("AssignedBase")
	if baseName then
		local base = basesFolder:FindFirstChild(baseName)
		if base then
			local spawnLoc = base:FindFirstChild("BaseSpawnLocation")
			if spawnLoc and spawnLoc:IsA("SpawnLocation") then
				spawnLoc.Enabled = false
			end
			local places = base:FindFirstChild("Places")
			if places then
				local startplace = places:FindFirstChild("startplace")
				if startplace and startplace:IsA("BasePart") then
					startplace.Transparency = 1
					startplace.CastShadow = false
				end
			end
			local baseValues = base:FindFirstChild("Values")
			if baseValues then
				local baseIncome = baseValues:FindFirstChild("Income")
				if baseIncome then
					baseIncome.Value = 0
				end
				local ownerValue = baseValues:FindFirstChild("Owner")
				if ownerValue and ownerValue:IsA("StringValue") and ownerValue.Value == player.Name then
					ownerValue.Value = ""
				end
			end
		end
	end
end)