Untitled

 avatar
unknown
plain_text
13 days ago
43 kB
3
Indexable
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Modülleri içe aktar
local TeamButtonModule = require(script.Parent.TeamButtonModule)
local GuiAnimationService = require(script.Parent.GuiAnimationService)

-- Oyuncu verileri
local playerTeamData = {}
local teamSwitchCooldowns = {}
local teamChangeConnections = {}
local playerPermanentTeam = {}
local lastHandledCharacters = {}

-- Hata ayıklama modu
local DEBUG_MODE = true
local function debugPrint(...)
	if DEBUG_MODE then
		print("[TeamSwitcher]", ...)
	end
end

-- Configuration
local CONFIG = {
	OPEN_KEY = Enum.KeyCode.T,
	MAIN_COLOR = Color3.fromRGB(211, 47, 47),
	SECONDARY_COLOR = Color3.fromRGB(30, 30, 30),
	ACCENT_COLOR = Color3.fromRGB(240, 240, 240),
	CORNER_RADIUS = UDim.new(0, 10),
	BUTTON_HOVER_SCALE = 1.05,
	BACKGROUND_IMAGE_ID = "rbxassetid://5843010904",
	MAIN_GROUP_ID = 35804679,
	STRICT_GROUP_CHECK = true,
	FORCE_SPAWN_LOCATION = true,
	RESPAWN_DELAY = 0.5,
	RESPAWN_HEIGHT = 4,
	SAVE_TEAMS_TO_ATTRIBUTES = true
}

-- Özel grup ID'leri
local SPECIFIC_GROUP_IDS = {
	JANDARMA_GROUP_ID = 35821336,
	ORDU_SUBAYLARI_GROUP_ID = 35824582,
	ORDU_GENERALLERI_GROUP_ID = 35824586,
	ISYANCILAR_GROUP_ID = CONFIG.MAIN_GROUP_ID + 3,
	ASKERI_INZIBAT_GROUP_ID = 35824215,
	OZEL_KUVVETLER_GROUP_ID = 35824297,
	HAVA_KUVVETLERI_GROUP_ID = 35824206,
	DONANMA_KUVVETLERI_GROUP_ID = 35824208,
	KARA_KUVVETLERI_GROUP_ID = 35824213,
	SINIR_MUFETTISLERI_GROUP_ID = 35824299
}

-- Alt grup ID'leri
local SUB_GROUP_IDS = {
	SPECIFIC_GROUP_IDS.ORDU_SUBAYLARI_GROUP_ID,
	SPECIFIC_GROUP_IDS.ORDU_GENERALLERI_GROUP_ID,
	SPECIFIC_GROUP_IDS.ISYANCILAR_GROUP_ID,
	SPECIFIC_GROUP_IDS.ASKERI_INZIBAT_GROUP_ID,
	SPECIFIC_GROUP_IDS.OZEL_KUVVETLER_GROUP_ID,
	SPECIFIC_GROUP_IDS.HAVA_KUVVETLERI_GROUP_ID,
	SPECIFIC_GROUP_IDS.JANDARMA_GROUP_ID,
	SPECIFIC_GROUP_IDS.DONANMA_KUVVETLERI_GROUP_ID,
	SPECIFIC_GROUP_IDS.KARA_KUVVETLERI_GROUP_ID,
	SPECIFIC_GROUP_IDS.SINIR_MUFETTISLERI_GROUP_ID
}

-- Team Configurations with Group Integration
local TEAMS = {
	{
		name = "Türk Silahlı Kuvvetleri",
		color = Color3.fromRGB(211, 47, 47),
		description = "Türk Silahlı Kuvvetleri (TSK)",
		spawnName = "Türk Silahlı KuvvetleriSpawn",
		icon = "rbxassetid://140528649686916",
		groupId = CONFIG.MAIN_GROUP_ID,
		isMainTeam = true
	},
	{
		name = "Ordu Subayları",
		color = Color3.fromRGB(45, 65, 40),
		description = "TSK Ordu Subayları",
		spawnName = "Ordu SubaylarıSpawn",
		icon = "rbxassetid://138263973754691",
		groupId = SPECIFIC_GROUP_IDS.ORDU_SUBAYLARI_GROUP_ID
	},
	{
		name = "Ordu Generalleri",
		color = Color3.fromRGB(112, 25, 25),
		description = "TSK Ordu Generalleri",
		spawnName = "Ordu GeneralleriSpawn",
		icon = "rbxassetid://138757980815031",
		groupId = SPECIFIC_GROUP_IDS.ORDU_GENERALLERI_GROUP_ID
	},
	{
		name = "Siviller",
		color = Color3.fromRGB(155, 155, 155),
		description = "Sivil Halk",
		spawnName = "SivillerSpawn",
		icon = "rbxassetid://110636224788748",
		groupId = 0,
		isPublicTeam = true
	},
	{
		name = "İsyancılar",
		color = Color3.fromRGB(82, 0, 0),
		description = "İsyancı Gruplar",
		spawnName = "İsyancılarSpawn",
		icon = "rbxassetid://78830197031582",
		groupId = SPECIFIC_GROUP_IDS.ISYANCILAR_GROUP_ID
	},
	{
		name = "Askeri İnzibat",
		color = Color3.fromRGB(34, 34, 34),
		description = "Askeri İnzibat (Askeri Polis)",
		spawnName = "Askeri İnzibatSpawn",
		icon = "rbxassetid://102987656736272",
		groupId = SPECIFIC_GROUP_IDS.ASKERI_INZIBAT_GROUP_ID
	},
	{
		name = "Özel Kuvvetler Komutanlığı",
		color = Color3.fromRGB(78, 42, 132),
		description = "Özel Kuvvetler Komutanlığı (ÖKK)",
		spawnName = "Özel Kuvvetler KomutanlığıSpawn",
		icon = "rbxassetid://86678101362245",
		groupId = SPECIFIC_GROUP_IDS.OZEL_KUVVETLER_GROUP_ID
	},
	{
		name = "Hava Kuvvetleri Komutanlığı",
		color = Color3.fromRGB(58, 83, 155),
		description = "Türk Hava Kuvvetleri Komutanlığı",
		spawnName = "Hava Kuvvetleri KomutanlığıSpawn",
		icon = "rbxassetid://86801105203862",
		groupId = SPECIFIC_GROUP_IDS.HAVA_KUVVETLERI_GROUP_ID
	},
	{
		name = "Jandarma Genel Komutanlığı",
		color = Color3.fromRGB(26, 83, 92),
		description = "Jandarma Genel Komutanlığı (JGK)",
		spawnName = "Jandarma Genel KomutanlığıSpawn",
		icon = "rbxassetid://81501472740069",
		groupId = SPECIFIC_GROUP_IDS.JANDARMA_GROUP_ID
	},
	{
		name = "Donanma Kuvvetleri Komutanlığı",
		color = Color3.fromRGB(24, 61, 93),
		description = "Türk Donanma Kuvvetleri Komutanlığı",
		spawnName = "Donanma Kuvvetleri KomutanlığıSpawn",
		icon = "rbxassetid://87731845578519",
		groupId = SPECIFIC_GROUP_IDS.DONANMA_KUVVETLERI_GROUP_ID
	},
	{
		name = "Kara Kuvvetleri Komutanlığı",
		color = Color3.fromRGB(39, 55, 77),
		description = "Türk Kara Kuvvetleri Komutanlığı",
		spawnName = "Kara Kuvvetleri KomutanlığıSpawn",
		icon = "rbxassetid://140025613264917",
		groupId = SPECIFIC_GROUP_IDS.KARA_KUVVETLERI_GROUP_ID
	},
	{
		name = "Sınır Müfettişleri",
		color = Color3.fromRGB(0, 100, 0),
		description = "Sınır Güvenliği Müfettişleri",
		spawnName = "Sınır MüfettişleriSpawn",
		icon = "rbxassetid://85035782865397",
		groupId = SPECIFIC_GROUP_IDS.SINIR_MUFETTISLERI_GROUP_ID
	}
}

-- Takım adı ile grup ID'sini eşleştiren tablo - hızlı arama için
local TEAM_NAME_TO_GROUP_ID = {}
for _, teamData in ipairs(TEAMS) do
	if teamData.groupId > 0 and not teamData.isMainTeam then
		TEAM_NAME_TO_GROUP_ID[teamData.name] = teamData.groupId
	end
end

-- Grup ID'si ile takım adını eşleştiren tablo - hızlı arama için
local GROUP_ID_TO_TEAM_NAME = {}
for teamName, groupId in pairs(TEAM_NAME_TO_GROUP_ID) do
	GROUP_ID_TO_TEAM_NAME[groupId] = teamName
end

-- Takım önbelleği
local teamCache = {}

-- Spawn Lokasyonları
local spawnLocationsCache = {}

-- Oyuncunun takım bilgisini attribute'larda sakla
local function savePlayerTeamToAttributes(player, teamName, spawnName)
	if CONFIG.SAVE_TEAMS_TO_ATTRIBUTES and player then
		player:SetAttribute("CurrentTeam", teamName)
		player:SetAttribute("CurrentSpawn", spawnName)
		debugPrint("Oyuncu takım bilgisi attribute'a kaydedildi: " .. player.Name .. " -> " .. teamName)
	end
end

-- Oyuncunun takım bilgisini attribute'lardan yükle
local function loadPlayerTeamFromAttributes(player)
	if CONFIG.SAVE_TEAMS_TO_ATTRIBUTES and player then
		local savedTeam = player:GetAttribute("CurrentTeam")
		local savedSpawn = player:GetAttribute("CurrentSpawn")

		if savedTeam and savedSpawn then
			debugPrint("Oyuncu takım bilgisi attribute'tan yüklendi: " .. player.Name .. " -> " .. savedTeam)
			return {
				teamName = savedTeam,
				spawnName = savedSpawn
			}
		end
	end

	return nil
end

-- Oyuncunun kalıcı takımını ayarla
local function setPermanentTeam(player, teamName, spawnName)
	if not player then return end

	playerPermanentTeam[player.UserId] = {
		teamName = teamName,
		spawnName = spawnName
	}

	savePlayerTeamToAttributes(player, teamName, spawnName)

	debugPrint("Oyuncunun kalıcı takımı ayarlandı: " .. player.Name .. " -> " .. teamName)
end

-- Oyuncunun kalıcı takımını al
local function getPermanentTeam(player)
	if not player then return nil end

	if playerPermanentTeam[player.UserId] then
		return playerPermanentTeam[player.UserId]
	end

	local attributeData = loadPlayerTeamFromAttributes(player)
	if attributeData then
		playerPermanentTeam[player.UserId] = attributeData
		return attributeData
	end

	return nil
end

-- Bul ve önbelleğe alma
local function findRespawnLocation(spawnName)
	if spawnLocationsCache[spawnName] then
		return spawnLocationsCache[spawnName]
	end

	local spawn = nil

	local spawnFolder = workspace:FindFirstChild("SpawnLocations")
	if spawnFolder then
		spawn = spawnFolder:FindFirstChild(spawnName)
	end

	if not spawn then
		spawn = workspace:FindFirstChild(spawnName)
	end

	if not spawn then
		debugPrint("Spawn noktası bulunamadı, oluşturuluyor: " .. spawnName)

		local color = Color3.fromRGB(200, 200, 200)
		for _, team in ipairs(TEAMS) do
			if team.spawnName == spawnName then
				color = team.color
				break
			end
		end

		spawn = Instance.new("SpawnLocation")
		spawn.Name = spawnName
		spawn.Size = Vector3.new(8, 1, 8)
		spawn.Position = Vector3.new(0, 10, 0)
		spawn.Anchored = true
		spawn.CanCollide = true
		spawn.Material = Enum.Material.SmoothPlastic
		spawn.BrickColor = BrickColor.new(color)
		spawn.Neutral = true

		if spawnFolder then
			spawn.Parent = spawnFolder
		else
			spawn.Parent = workspace
		end
	end

	spawn.Neutral = true

	spawnLocationsCache[spawnName] = spawn
	return spawn
end

-- Takımı önbellekten al veya oluştur
local function getTeam(teamName, teamColor)
	if teamCache[teamName] then
		return teamCache[teamName]
	end

	local team = Teams:FindFirstChild(teamName)
	if not team then
		team = Instance.new("Team")
		team.Name = teamName
		team.TeamColor = BrickColor.new(teamColor)
		team.AutoAssignable = false
		team.Parent = Teams
	else
		team.AutoAssignable = false
	end

	teamCache[teamName] = team
	return team
end

-- İsme göre takım veri nesnesini bul
local function getTeamDataByName(teamName)
	for _, teamData in ipairs(TEAMS) do
		if teamData.name == teamName then
			return teamData
		end
	end
	return nil
end

-- Güvenli teleport fonksiyonu
local function teleportToSpawn(player, spawnName)
	if not player or not player.Character then 
		debugPrint("Teleport başarısız: Karakter bulunamadı - " .. player.Name)
		return false 
	end

	local spawn = findRespawnLocation(spawnName)
	if not spawn then 
		debugPrint("Teleport başarısız: Spawn bulunamadı - " .. spawnName)
		return false 
	end

	local hrp = player.Character:FindFirstChild("HumanoidRootPart")
	if not hrp then 
		debugPrint("Teleport başarısız: HumanoidRootPart bulunamadı - " .. player.Name)
		return false 
	end

	local success, err = pcall(function()
		hrp.CFrame = spawn.CFrame * CFrame.new(0, CONFIG.RESPAWN_HEIGHT, 0)
	end)

	if not success then
		debugPrint("Teleport hatası: " .. tostring(err))
		return false
	end

	debugPrint("Teleport başarılı: " .. player.Name .. " -> " .. spawnName)
	return true
end

-- Asset ID doğrulama
local function validateAssetId(assetId)
	if not assetId or assetId == "" then
		return "rbxassetid://0"
	end

	if not string.match(assetId, "^rbxassetid://[0-9]+$") then
		debugPrint("Geçersiz asset ID formatı: " .. assetId)
		return "rbxassetid://0"
	end

	return assetId
end

-- Oyuncunun herhangi bir grupta olup olmadığını kontrol eden fonksiyon
local function isInAnyGroup(player)
	if not player then return false, nil end

	local success, isInMainGroup = pcall(function()
		return player:IsInGroup(CONFIG.MAIN_GROUP_ID)
	end)

	if success and isInMainGroup then
		return true, CONFIG.MAIN_GROUP_ID
	end

	for _, groupId in ipairs(SUB_GROUP_IDS) do
		local success, inGroup = pcall(function()
			return player:IsInGroup(groupId)
		end)

		if success and inGroup then
			return true, groupId
		end
	end

	return false, nil
end

-- Oyuncunun herhangi bir alt grupta olup olmadığını kontrol eden fonksiyon
local function isInAnySubGroup(player)
	if not player then return false, nil end

	for _, groupId in ipairs(SUB_GROUP_IDS) do
		local success, inGroup = pcall(function()
			return player:IsInGroup(groupId)
		end)

		if success and inGroup then
			return true, groupId
		end
	end

	return false, nil
end

-- Oyuncunun ana grupta olup olmadığını kontrol eden fonksiyon
local function isInMainGroup(player)
	if not player then return false end

	local success, isInGroup = pcall(function()
		return player:IsInGroup(CONFIG.MAIN_GROUP_ID)
	end)

	return success and isInGroup
end

-- Oyuncunun belirli bir grupta olup olmadığını kontrol eden fonksiyon
local function isInSpecificGroup(player, groupId)
	if not player then return false end

	local success, isInGroup = pcall(function()
		return player:IsInGroup(groupId)
	end)

	return success and isInGroup
end

-- Oyuncunun belirli bir takıma geçiş yapıp yapamayacağını kontrol eden fonksiyon
local function canJoinTeam(player, teamData)
	if not player or not teamData then return false end

	if teamData.isPublicTeam or teamData.name == "Siviller" then
		local inAnyGroup, _ = isInAnyGroup(player)
		return not inAnyGroup
	end

	if teamData.isMainTeam then
		local inAnyGroup, _ = isInAnyGroup(player)
		return inAnyGroup
	end

	if CONFIG.STRICT_GROUP_CHECK and teamData.groupId > 0 then
		return isInSpecificGroup(player, teamData.groupId)
	end

	return false
end

-- Takım değiştirme sınırlaması
local function canSwitchTeam(player)
	if not player then return false, "Oyuncu bulunamadı" end

	local lastSwitch = teamSwitchCooldowns[player.UserId]
	local cooldownTime = 60

	if lastSwitch and (tick() - lastSwitch) < cooldownTime then
		return false, "Takım değiştirmek için " .. math.ceil(cooldownTime - (tick() - lastSwitch)) .. " saniye beklemelisiniz."
	end

	return true, ""
end

-- Cooldown'ı kaydet
local function recordTeamSwitch(player)
	if not player then return end
	teamSwitchCooldowns[player.UserId] = tick()
end

-- Oyuncunun takımını değiştir
local function switchTeam(player, teamName)
	if not player or not teamName then 
		debugPrint("Takım değiştirme başarısız: Geçersiz parametre")
		return false
	end

	debugPrint("Takım değiştirme işlemi: " .. player.Name .. " -> " .. teamName)

	local teamData = getTeamDataByName(teamName)
	if not teamData then
		debugPrint("Takım bulunamadı: " .. teamName)
		return false
	end

	local team = getTeam(teamName, teamData.color)
	player.Team = team

	playerTeamData[player.UserId] = {
		teamName = teamName,
		spawnName = teamData.spawnName
	}

	setPermanentTeam(player, teamName, teamData.spawnName)

	debugPrint("Oyuncu takıma atandı: " .. player.Name .. " -> " .. teamName)
	return true
end

-- Teleport işlemini yapan fonksiyon
local function teleportPlayerToTeamSpawn(player)
	if not player or not player.Character then return end

	local teamData = nil

	if player.Team then
		teamData = getTeamDataByName(player.Team.Name)
	end

	if not teamData then
		local savedTeamInfo = getPermanentTeam(player)
		if savedTeamInfo then
			teamData = getTeamDataByName(savedTeamInfo.teamName)
		end
	end

	if not teamData then
		local inAnyGroup = isInAnyGroup(player)
		if inAnyGroup then
			teamData = getTeamDataByName("Türk Silahlı Kuvvetleri")
		else
			teamData = getTeamDataByName("Siviller")
		end
	end

	if teamData and teamData.spawnName then
		teleportToSpawn(player, teamData.spawnName)
	end
end

-- Takım belirleme
local function determineInitialTeam(player)
	if not player then return "Siviller" end

	local inAnyGroup, _ = isInAnyGroup(player)
	if inAnyGroup then
		return "Türk Silahlı Kuvvetleri"
	end

	return "Siviller"
end

-- Function to create the main GUI structure
local function createTeamSwitcherGui(player)
	if not player then return nil end

	local existingGui = player.PlayerGui:FindFirstChild("TeamSwitcherGui")
	if existingGui then
		existingGui:Destroy()
	end

	local screenGui = Instance.new("ScreenGui")
	screenGui.Name = "TeamSwitcherGui"
	screenGui.ResetOnSpawn = false
	screenGui.DisplayOrder = 100
	screenGui.Enabled = false

	local backgroundFrame = Instance.new("Frame")
	backgroundFrame.Name = "BackgroundFrame"
	backgroundFrame.Size = UDim2.new(1, 0, 1, 0)
	backgroundFrame.Position = UDim2.new(0, 0, 0, 0)
	backgroundFrame.BackgroundColor3 = Color3.new(0, 0, 0)
	backgroundFrame.BackgroundTransparency = 1
	backgroundFrame.BorderSizePixel = 0
	backgroundFrame.Parent = screenGui

	local mainFrame = Instance.new("Frame")
	mainFrame.Name = "MainFrame"
	mainFrame.Size = UDim2.new(0.5, 0, 0.55, 0)
	mainFrame.Position = UDim2.new(0.5, 0, 0.5, 0)
	mainFrame.AnchorPoint = Vector2.new(0.5, 0.5)
	mainFrame.BackgroundColor3 = CONFIG.SECONDARY_COLOR
	mainFrame.BorderSizePixel = 0

	local mainBackground = Instance.new("ImageLabel")
	mainBackground.Name = "MainBackground"
	mainBackground.Size = UDim2.new(1, 0, 1, 0)
	mainBackground.Position = UDim2.new(0, 0, 0, 0)
	mainBackground.BackgroundTransparency = 1
	mainBackground.Image = validateAssetId(CONFIG.BACKGROUND_IMAGE_ID)
	mainBackground.ImageColor3 = Color3.fromRGB(20, 20, 20)
	mainBackground.ImageTransparency = 0.2
	mainBackground.ScaleType = Enum.ScaleType.Stretch
	mainBackground.ZIndex = 0

	debugPrint("Arka plan resmi yükleniyor: " .. CONFIG.BACKGROUND_IMAGE_ID)

	mainBackground.Parent = mainFrame

	local mainFrameGradient = Instance.new("UIGradient")
	mainFrameGradient.Rotation = 45
	mainFrameGradient.Color = ColorSequence.new({
		ColorSequenceKeypoint.new(0, Color3.fromRGB(35, 35, 35)),
		ColorSequenceKeypoint.new(1, Color3.fromRGB(25, 25, 25))
	})
	mainFrameGradient.Transparency = NumberSequence.new({
		NumberSequenceKeypoint.new(0, 0.4),
		NumberSequenceKeypoint.new(1, 0.7)
	})
	mainFrameGradient.Parent = mainFrame

	mainFrame.Parent = screenGui

	local uiCorner = Instance.new("UICorner")
	uiCorner.CornerRadius = CONFIG.CORNER_RADIUS
	uiCorner.Parent = mainFrame

	local border = Instance.new("UIStroke")
	border.Color = Color3.fromRGB(80, 80, 80)
	border.Thickness = 1.5
	border.Transparency = 0.3
	border.Parent = mainFrame

	local shadowFrame = Instance.new("Frame")
	shadowFrame.Name = "Shadow"
	shadowFrame.Size = UDim2.new(1, 10, 1, 10)
	shadowFrame.Position = UDim2.new(0.5, 0, 0.5, 0)
	shadowFrame.AnchorPoint = Vector2.new(0.5, 0.5)
	shadowFrame.BackgroundColor3 = Color3.new(0, 0, 0)
	shadowFrame.BackgroundTransparency = 0.6
	shadowFrame.BorderSizePixel = 0
	shadowFrame.ZIndex = -1

	local shadowCorner = Instance.new("UICorner")
	shadowCorner.CornerRadius = UDim.new(0, 14)
	shadowCorner.Parent = shadowFrame

	local shadowBlur = Instance.new("UIGradient")
	shadowBlur.Rotation = 90
	shadowBlur.Transparency = NumberSequence.new({
		NumberSequenceKeypoint.new(0, 0.7),
		NumberSequenceKeypoint.new(0.5, 0.8),
		NumberSequenceKeypoint.new(1, 0.9)
	})
	shadowBlur.Parent = shadowFrame

	shadowFrame.Parent = mainFrame

	local titleBar = Instance.new("Frame")
	titleBar.Name = "TitleBar"
	titleBar.Size = UDim2.new(1, 0, 0.08, 0)
	titleBar.Position = UDim2.new(0, 0, 0, 0)
	titleBar.BackgroundColor3 = Color3.fromRGB(20, 20, 20)
	titleBar.BackgroundTransparency = 0.2
	titleBar.BorderSizePixel = 0
	titleBar.ZIndex = 2
	titleBar.Parent = mainFrame

	local titleGradient = Instance.new("UIGradient")
	titleGradient.Rotation = 90
	titleGradient.Color = ColorSequence.new({
		ColorSequenceKeypoint.new(0, Color3.fromRGB(40, 40, 40)),
		ColorSequenceKeypoint.new(1, Color3.fromRGB(20, 20, 20))
	})
	titleGradient.Parent = titleBar

	local titleCorner = Instance.new("UICorner")
	titleCorner.CornerRadius = CONFIG.CORNER_RADIUS
	titleCorner.Parent = titleBar

	local bottomFrame = Instance.new("Frame")
	bottomFrame.Name = "BottomFrame"
	bottomFrame.Size = UDim2.new(1, 0, 0.5, 0)
	bottomFrame.Position = UDim2.new(0, 0, 0.5, 0)
	bottomFrame.BackgroundColor3 = Color3.fromRGB(20, 20, 20)
	bottomFrame.BackgroundTransparency = 0.2
	bottomFrame.BorderSizePixel = 0
	bottomFrame.ZIndex = 2
	bottomFrame.Parent = titleBar

	local bottomGradient = titleGradient:Clone()
	bottomGradient.Parent = bottomFrame

	local titleBackground = Instance.new("Frame")
	titleBackground.Name = "TitleBackground"
	titleBackground.Size = UDim2.new(0.35, 0, 1.2, 0)
	titleBackground.Position = UDim2.new(0.05, 0, -0.1, 0)
	titleBackground.BackgroundColor3 = CONFIG.MAIN_COLOR
	titleBackground.BorderSizePixel = 0
	titleBackground.ZIndex = 3
	titleBackground.Parent = titleBar

	local titleBgGradient = Instance.new("UIGradient")
	titleBgGradient.Rotation = 45
	titleBgGradient.Color = ColorSequence.new({
		ColorSequenceKeypoint.new(0, Color3.fromRGB(230, 60, 60)),
		ColorSequenceKeypoint.new(1, Color3.fromRGB(170, 30, 30))
	})
	titleBgGradient.Parent = titleBackground

	local titleBgCorner = Instance.new("UICorner")
	titleBgCorner.CornerRadius = UDim.new(0, 8)
	titleBgCorner.Parent = titleBackground

	local titleText = Instance.new("TextLabel")
	titleText.Name = "TitleText"
	titleText.Size = UDim2.new(0.9, 0, 0.9, 0)
	titleText.Position = UDim2.new(0.05, 0, 0.05, 0)
	titleText.BackgroundTransparency = 1
	titleText.Text = "TAKIMLAR"
	titleText.Font = Enum.Font.GothamBlack
	titleText.TextColor3 = Color3.new(1, 1, 1)
	titleText.TextSize = 22
	titleText.ZIndex = 4

	local textStroke = Instance.new("UIStroke")
	textStroke.Color = Color3.fromRGB(0, 0, 0)
	textStroke.Thickness = 1
	textStroke.Transparency = 0.5
	textStroke.Parent = titleText

	titleText.Parent = titleBackground

	local iconContainer = Instance.new("Frame")
	iconContainer.Name = "IconContainer"
	iconContainer.Size = UDim2.new(0.08, 0, 0.8, 0)
	iconContainer.Position = UDim2.new(0.24, 0, 0.1, 0)
	iconContainer.BackgroundTransparency = 1
	iconContainer.ZIndex = 4
	iconContainer.Parent = titleBackground

	local teamIcon = Instance.new("ImageLabel")
	teamIcon.Name = "TeamIcon"
	teamIcon.Size = UDim2.new(1, 0, 1, 0)
	teamIcon.BackgroundTransparency = 1
	teamIcon.Image = validateAssetId("rbxassetid://7743845014")
	teamIcon.ScaleType = Enum.ScaleType.Fit
	teamIcon.ZIndex = 4
	teamIcon.Parent = iconContainer

	local tsoTitle = Instance.new("TextLabel")
	tsoTitle.Name = "TSOTitle"
	tsoTitle.Size = UDim2.new(0.35, 0, 0.8, 0)
	tsoTitle.Position = UDim2.new(0.50, 0, 0.1, 0)
	tsoTitle.BackgroundTransparency = 1
	tsoTitle.Text = "|TSO| Türk Silahlı Ordusu"
	tsoTitle.Font = Enum.Font.GothamBold
	tsoTitle.TextColor3 = Color3.fromRGB(200, 200, 200)
	tsoTitle.TextSize = 16
	tsoTitle.TextXAlignment = Enum.TextXAlignment.Right
	tsoTitle.ZIndex = 4
	tsoTitle.Parent = titleBar

	local tsoTextStroke = Instance.new("UIStroke")
	tsoTextStroke.Color = Color3.fromRGB(0, 0, 0)
	tsoTextStroke.Thickness = 1
	tsoTextStroke.Transparency = 0.7
	tsoTextStroke.Parent = tsoTitle

	local closeButton = Instance.new("TextButton")
	closeButton.Name = "CloseButton"
	closeButton.Size = UDim2.new(0.06, 0, 0.8, 0)
	closeButton.Position = UDim2.new(0.94, 0, 0.1, 0)
	closeButton.AnchorPoint = Vector2.new(0.5, 0)
	closeButton.BackgroundColor3 = Color3.fromRGB(200, 50, 50)
	closeButton.BorderSizePixel = 0
	closeButton.Text = "X"
	closeButton.Font = Enum.Font.GothamBold
	closeButton.TextColor3 = Color3.fromRGB(255, 255, 255)
	closeButton.TextSize = 20
	closeButton.ZIndex = 4
	closeButton.Parent = titleBar

	local closeTextStroke = Instance.new("UIStroke")
	closeTextStroke.Color = Color3.fromRGB(0, 0, 0)
	closeTextStroke.Thickness = 1
	closeTextStroke.Transparency = 0.7
	closeTextStroke.Parent = closeButton

	local closeGradient = Instance.new("UIGradient")
	closeGradient.Rotation = 90
	closeGradient.Color = ColorSequence.new({
		ColorSequenceKeypoint.new(0, Color3.fromRGB(220, 70, 70)),
		ColorSequenceKeypoint.new(1, Color3.fromRGB(180, 40, 40))
	})
	closeGradient.Parent = closeButton

	local closeCorner = Instance.new("UICorner")
	closeCorner.CornerRadius = UDim.new(0, 6)
	closeCorner.Parent = closeButton

	closeButton.MouseEnter:Connect(function()
		closeGradient.Color = ColorSequence.new({
			ColorSequenceKeypoint.new(0, Color3.fromRGB(255, 80, 80)),
			ColorSequenceKeypoint.new(1, Color3.fromRGB(200, 50, 50))
		})

		closeButton.TextColor3 = Color3.fromRGB(255, 255, 255)
		closeTextStroke.Transparency = 0.5
	end)

	closeButton.MouseLeave:Connect(function()
		closeGradient.Color = ColorSequence.new({
			ColorSequenceKeypoint.new(0, Color3.fromRGB(220, 70, 70)),
			ColorSequenceKeypoint.new(1, Color3.fromRGB(180, 40, 40))
		})

		closeButton.TextColor3 = Color3.fromRGB(255, 255, 255)
		closeTextStroke.Transparency = 0.7
	end)

	local contentFrame = Instance.new("Frame")
	contentFrame.Name = "ContentFrame"
	contentFrame.Size = UDim2.new(1, 0, 0.92, 0)
	contentFrame.Position = UDim2.new(0, 0, 0.08, 0)
	contentFrame.BackgroundColor3 = Color3.fromRGB(35, 35, 35)
	contentFrame.BackgroundTransparency = 0.4
	contentFrame.BorderSizePixel = 0
	contentFrame.ZIndex = 1
	contentFrame.Parent = mainFrame

	local contentGradient = Instance.new("UIGradient")
	contentGradient.Rotation = 135
	contentGradient.Color = ColorSequence.new({
		ColorSequenceKeypoint.new(0, Color3.fromRGB(40, 40, 40)),
		ColorSequenceKeypoint.new(1, Color3.fromRGB(30, 30, 30))
	})
	contentGradient.Transparency = NumberSequence.new({
		NumberSequenceKeypoint.new(0, 0.4),
		NumberSequenceKeypoint.new(1, 0.6)
	})
	contentGradient.Parent = contentFrame

	local teamButtonsContainer = Instance.new("Frame")
	teamButtonsContainer.Name = "TeamButtonsContainer"
	teamButtonsContainer.Size = UDim2.new(0.95, 0, 0.9, 0)
	teamButtonsContainer.Position = UDim2.new(0.5, 0, 0.5, 0)
	teamButtonsContainer.AnchorPoint = Vector2.new(0.5, 0.5)
	teamButtonsContainer.BackgroundTransparency = 1
	teamButtonsContainer.ZIndex = 2
	teamButtonsContainer.Parent = contentFrame

	local gridLayout = Instance.new("UIGridLayout")
	gridLayout.CellSize = UDim2.new(0.235, 0, 0.235, 0)
	gridLayout.CellPadding = UDim2.new(0.018, 0, 0.018, 0)
	gridLayout.SortOrder = Enum.SortOrder.LayoutOrder
	gridLayout.HorizontalAlignment = Enum.HorizontalAlignment.Center
	gridLayout.VerticalAlignment = Enum.VerticalAlignment.Center
	gridLayout.Parent = teamButtonsContainer

	local scrollingFrame = Instance.new("ScrollingFrame")
	scrollingFrame.Name = "ScrollingFrame"
	scrollingFrame.Size = UDim2.new(1, 0, 1, 0)
	scrollingFrame.Position = UDim2.new(0, 0, 0, 0)
	scrollingFrame.BackgroundTransparency = 1
	scrollingFrame.ScrollBarThickness = 4
	scrollingFrame.ScrollBarImageColor3 = Color3.fromRGB(180, 180, 180)
	scrollingFrame.ScrollingDirection = Enum.ScrollingDirection.Y
	scrollingFrame.AutomaticCanvasSize = Enum.AutomaticSize.Y
	scrollingFrame.CanvasSize = UDim2.new(0, 0, 0, 0)
	scrollingFrame.ZIndex = 3
	scrollingFrame.Parent = teamButtonsContainer

	gridLayout.Parent = scrollingFrame

	local inMainGroup = isInMainGroup(player)
	local inAnySubGroup, subGroupId = isInAnySubGroup(player)

	local playerSpecificGroups = {}
	for groupId, teamName in pairs(GROUP_ID_TO_TEAM_NAME) do
		if isInSpecificGroup(player, groupId) then
			table.insert(playerSpecificGroups, {
				groupId = groupId,
				teamName = teamName
			})
		end
	end

	for i, teamData in ipairs(TEAMS) do
		local canJoin = canJoinTeam(player, teamData)
		local teamButton = TeamButtonModule.createTeamButton(teamData, i, canJoin)

		teamButton:SetAttribute("CanJoin", canJoin)
		teamButton:SetAttribute("TeamName", teamData.name)
		teamButton:SetAttribute("TeamColor", teamData.color)
		teamButton:SetAttribute("SpawnName", teamData.spawnName)

		teamButton.Parent = scrollingFrame
	end

	local infoFrame = Instance.new("Frame")
	infoFrame.Name = "InfoFrame"
	infoFrame.Size = UDim2.new(1, 0, 0.06, 0)
	infoFrame.Position = UDim2.new(0, 0, 0.94, 0)
	infoFrame.BackgroundColor3 = Color3.fromRGB(20, 20, 20)
	infoFrame.BackgroundTransparency = 0.3
	infoFrame.BorderSizePixel = 0
	infoFrame.ZIndex = 3
	infoFrame.Parent = contentFrame

	local infoGradient = Instance.new("UIGradient")
	infoGradient.Rotation = 90
	infoGradient.Color = ColorSequence.new({
		ColorSequenceKeypoint.new(0, Color3.fromRGB(40, 40, 40)),
		ColorSequenceKeypoint.new(1, Color3.fromRGB(25, 25, 25))
	})
	infoGradient.Parent = infoFrame

	local infoText = Instance.new("TextLabel")
	infoText.Name = "InfoText"
	infoText.Size = UDim2.new(1, -20, 1, 0)
	infoText.Position = UDim2.new(0.5, 0, 0, 0)
	infoText.AnchorPoint = Vector2.new(0.5, 0)
	infoText.BackgroundTransparency = 1
	infoText.Font = Enum.Font.Gotham
	infoText.TextColor3 = Color3.fromRGB(200, 200, 200)
	infoText.TextSize = 14
	infoText.TextWrapped = true
	infoText.TextXAlignment = Enum.TextXAlignment.Center
	infoText.TextYAlignment = Enum.TextYAlignment.Center
	infoText.ZIndex = 4

	if inMainGroup then
		infoText.Text = "TSO ana grup üyesisiniz. TSK takımına katılabilirsiniz."
	elseif #playerSpecificGroups > 0 then
		local groupInfo = playerSpecificGroups[1]
		infoText.Text = groupInfo.teamName .. " grup üyesisiniz. Bu takıma katılabilirsiniz."
	elseif inAnySubGroup then
		local subGroupName = "bilinmeyen birim"
		for _, teamData in ipairs(TEAMS) do
			if teamData.groupId == subGroupId then
				subGroupName = teamData.name
				break
			end
		end
		infoText.Text = subGroupName .. " grup üyesisiniz. Kendi takımınıza katılabilirsiniz."
	else
		infoText.Text = "TSO grubuna üye değilsiniz. Sadece Siviller takımına katılabilirsiniz."
	end

	infoText.Parent = infoFrame

	screenGui.Parent = player:WaitForChild("PlayerGui")
	return screenGui
end

-- Toggle GUI için ayrı bir ScreenGui oluştur
local function createToggleGui(player)
	if not player then return nil, nil end

	local existingToggleGui = player.PlayerGui:FindFirstChild("TeamSwitcherToggleGui")
	if existingToggleGui then
		existingToggleGui:Destroy()
	end

	local toggleGui = Instance.new("ScreenGui")
	toggleGui.Name = "TeamSwitcherToggleGui"
	toggleGui.ResetOnSpawn = false
	toggleGui.DisplayOrder = 100
	toggleGui.Parent = player.PlayerGui

	local toggleButton = Instance.new("ImageButton")
	toggleButton.Name = "TeamSwitcherToggle"
	toggleButton.Size = UDim2.new(0, 60, 0, 60)
	toggleButton.Position = UDim2.new(0.013, 0, 0.463, 0)
	toggleButton.AnchorPoint = Vector2.new(0, 0)
	toggleButton.BackgroundColor3 = Color3.fromRGB(200, 40, 40)
	toggleButton.BorderSizePixel = 0
	toggleButton.AutoLocalize = false
	toggleButton.ZIndex = 3

	local bagIcon = Instance.new("ImageLabel")
	bagIcon.Name = "BagIcon"
	bagIcon.Size = UDim2.new(0.7, 0, 0.7, 0)
	bagIcon.Position = UDim2.new(0.5, 0, 0.5, 0)
	bagIcon.AnchorPoint = Vector2.new(0.5, 0.5)
	bagIcon.BackgroundTransparency = 1
	bagIcon.Image = validateAssetId("rbxassetid://126899905535187")
	bagIcon.ImageColor3 = Color3.fromRGB(255, 255, 255)
	bagIcon.ScaleType = Enum.ScaleType.Fit
	bagIcon.ZIndex = 4
	bagIcon.Parent = toggleButton

	local toggleGradient = Instance.new("UIGradient")
	toggleGradient.Rotation = 45
	toggleGradient.Color = ColorSequence.new({
		ColorSequenceKeypoint.new(0, Color3.fromRGB(220, 50, 50)),
		ColorSequenceKeypoint.new(1, Color3.fromRGB(180, 30, 30))
	})
	toggleGradient.Parent = toggleButton

	local toggleCorner = Instance.new("UICorner")
	toggleCorner.CornerRadius = UDim.new(0, 8)
	toggleCorner.Parent = toggleButton

	local toggleShadow = Instance.new("Frame")
	toggleShadow.Name = "Shadow"
	toggleShadow.Size = UDim2.new(1, 6, 1, 6)
	toggleShadow.Position = UDim2.new(0.5, 0, 0.5, 0)
	toggleShadow.AnchorPoint = Vector2.new(0.5, 0.5)
	toggleShadow.BackgroundColor3 = Color3.new(0, 0, 0)
	toggleShadow.BackgroundTransparency = 0.7
	toggleShadow.BorderSizePixel = 0
	toggleShadow.ZIndex = 2
	toggleShadow.Parent = toggleButton

	local toggleShadowCorner = Instance.new("UICorner")
	toggleShadowCorner.CornerRadius = UDim.new(0, 10)
	toggleShadowCorner.Parent = toggleShadow

	toggleButton.MouseEnter:Connect(function()
		toggleGradient.Color = ColorSequence.new({
			ColorSequenceKeypoint.new(0, Color3.fromRGB(240, 60, 60)),
			ColorSequenceKeypoint.new(1, Color3.fromRGB(200, 40, 40))
		})

		bagIcon.ImageColor3 = Color3.fromRGB(255, 255, 255)
	end)

	toggleButton.MouseLeave:Connect(function()
		toggleGradient.Color = ColorSequence.new({
			ColorSequenceKeypoint.new(0, Color3.fromRGB(220, 50, 50)),
			ColorSequenceKeypoint.new(1, Color3.fromRGB(180, 30, 30))
		})

		bagIcon.ImageColor3 = Color3.fromRGB(255, 255, 255)
	end)

	toggleButton.Parent = toggleGui

	return toggleGui, toggleButton
end

-- GUI'nin hazırlanması için ana fonksiyon
local function setupPlayerGui(player)
	if not player then return nil, nil end

	debugPrint("GUI kurulumu başlatılıyor: " .. player.Name)

	local gui = createTeamSwitcherGui(player)
	local toggleGui, toggleButton = createToggleGui(player)

	if not gui or not toggleGui or not toggleButton then
		debugPrint("GUI oluşturulamadı")
		return nil, nil
	end

	toggleButton.MouseButton1Click:Connect(function()
		if gui.Enabled then
			GuiAnimationService.closeGui(gui)
		else
			GuiAnimationService.openGui(gui)
		end
	end)

	local closeButton = gui.MainFrame.TitleBar.CloseButton
	closeButton.MouseButton1Click:Connect(function()
		GuiAnimationService.closeGui(gui)
	end)

	local backgroundFrame = gui.BackgroundFrame
	backgroundFrame.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			GuiAnimationService.closeGui(gui)
		end
	end)

	local scrollingFrame = gui.MainFrame.ContentFrame.TeamButtonsContainer.ScrollingFrame
	for _, teamButton in pairs(scrollingFrame:GetChildren()) do
		if teamButton:IsA("Frame") and teamButton.Name:find("TeamButton_") then
			local button = teamButton:FindFirstChild("SelectButton")
			if not button then continue end

			button.MouseEnter:Connect(function()
				if teamButton:GetAttribute("CanJoin") then
					GuiAnimationService.buttonHover(teamButton, true, teamButton:GetAttribute("TeamColor"))
				end
			end)

			button.MouseLeave:Connect(function()
				if teamButton:GetAttribute("CanJoin") then
					GuiAnimationService.buttonHover(teamButton, false)
				end
			end)

			button.MouseButton1Click:Connect(function()
				if teamButton:GetAttribute("CanJoin") then
					local teamName = teamButton:GetAttribute("TeamName")

					if teamName then
						local canSwitch, cooldownMessage = canSwitchTeam(player)
						if not canSwitch then
							local notification = Instance.new("TextLabel")
							notification.Name = "Notification"
							notification.Size = UDim2.new(0.4, 0, 0.1, 0)
							notification.Position = UDim2.new(0.5, 0, 0.9, 0)
							notification.AnchorPoint = Vector2.new(0.5, 1)
							notification.BackgroundColor3 = Color3.fromRGB(200, 100, 0)
							notification.BackgroundTransparency = 0.3
							notification.BorderSizePixel = 0
							notification.Text = cooldownMessage
							notification.Font = Enum.Font.GothamBold
							notification.TextColor3 = Color3.fromRGB(255, 255, 255)
							notification.TextSize = 16
							notification.ZIndex = 10

							local notiCorner = Instance.new("UICorner")
							notiCorner.CornerRadius = UDim.new(0, 6)
							notiCorner.Parent = notification

							notification.Parent = gui

							task.delay(3, function()
								if notification and notification.Parent then
									notification:Destroy()
								end
							end)

							return
						end

						recordTeamSwitch(player)

						if switchTeam(player, teamName) then
							local teamData = getTeamDataByName(teamName)
							if teamData and teamData.spawnName and player.Character then
								teleportToSpawn(player, teamData.spawnName)
							end

							debugPrint("Manuel takım geçişi: " .. player.Name .. " -> " .. teamName)
						else
							debugPrint("Takım değiştirmede sorun oluştu: " .. teamName)
						end

						GuiAnimationService.closeGui(gui)
					end
				else
					local teamName = teamButton:GetAttribute("TeamName")
					local teamData = nil
					for _, data in ipairs(TEAMS) do
						if data.name == teamName then
							teamData = data
							break
						end
					end

					if teamData then
						local message = ""

						if teamData.name == "Siviller" then
							message = "Siviller takımına sadece hiçbir grupta üye olmayanlar katılabilir."
						elseif teamData.isMainTeam then
							message = teamName .. " takımına katılmak için TSO ana grubuna veya bir alt gruba üye olmalısınız."
						elseif teamData.groupId > 0 then
							if TEAM_NAME_TO_GROUP_ID[teamData.name] then
								message = teamName .. " takımına katılmak için " .. teamData.name .. " grubuna üye olmalısınız."
							else
								message = "Bu takıma katılmak için ilgili gruba üye olmalısınız."
							end
						else
							message = "Bu takıma katılamazsınız."
						end

						local notification = Instance.new("TextLabel")
						notification.Name = "Notification"
						notification.Size = UDim2.new(0.4, 0, 0.1, 0)
						notification.Position = UDim2.new(0.5, 0, 0.9, 0)
						notification.AnchorPoint = Vector2.new(0.5, 1)
						notification.BackgroundColor3 = Color3.fromRGB(200, 0, 0)
						notification.BackgroundTransparency = 0.3
						notification.BorderSizePixel = 0
						notification.Text = message
						notification.Font = Enum.Font.GothamBold
						notification.TextColor3 = Color3.fromRGB(255, 255, 255)
						notification.TextSize = 16
						notification.ZIndex = 10

						local notiCorner = Instance.new("UICorner")
						notiCorner.CornerRadius = UDim.new(0, 6)
						notiCorner.Parent = notification

						notification.Parent = gui

						task.delay(3, function()
							if notification and notification.Parent then
								notification:Destroy()
							end
						end)
					end
				end
			end)
		end
	end

	local userInputService = game:GetService("UserInputService")
	local debounce = false

	local inputConnection = userInputService.InputBegan:Connect(function(input, gameProcessed)
		if not gameProcessed and input.KeyCode == CONFIG.OPEN_KEY and not debounce then
			debounce = true

			if gui.Enabled then
				GuiAnimationService.closeGui(gui)
			else
				GuiAnimationService.openGui(gui)
			end

			task.delay(0.5, function()
				debounce = false
			end)
		end
	end)

	player.AncestryChanged:Connect(function(_, parent)
		if parent == nil and inputConnection then
			inputConnection:Disconnect()
			inputConnection = nil
		end
	end)

	return gui, toggleGui
end

-- Takımları oluştur
local function initializeTeams()
	for _, teamData in ipairs(TEAMS) do
		getTeam(teamData.name, teamData.color)
	end

	debugPrint("Takımlar başarıyla oluşturuldu")

	for _, team in ipairs(Teams:GetTeams()) do
		team.AutoAssignable = false
	end

	Teams.ChildAdded:Connect(function(child)
		if child:IsA("Team") then
			child.AutoAssignable = false
		end
	end)

	debugPrint("Takım otomatik ataması devre dışı bırakıldı")
end

-- Admin komutu ile takım değişikliğini takip etmek için Team değişikliklerini izle
local function monitorTeamChanges()
	local function setupTeamChangeMonitor(player)
		if teamChangeConnections[player.UserId] then
			local connection = teamChangeConnections[player.UserId]
			if typeof(connection) == "RBXScriptConnection" then
				connection:Disconnect()
				teamChangeConnections[player.UserId] = nil
			end
		end

		local connection = player:GetPropertyChangedSignal("Team"):Connect(function()
			if player.Team then
				local teamName = player.Team.Name

				local teamData = getTeamDataByName(teamName)
				if teamData and teamData.spawnName then
					playerTeamData[player.UserId] = {
						teamName = teamName,
						spawnName = teamData.spawnName
					}

					setPermanentTeam(player, teamName, teamData.spawnName)

					debugPrint("Takım değişikliği algılandı: " .. player.Name .. " -> " .. teamName)

					if player.Character then
						teleportToSpawn(player, teamData.spawnName)
					end
				end
			end
		end)

		teamChangeConnections[player.UserId] = connection
	end

	for _, player in pairs(Players:GetPlayers()) do
		setupTeamChangeMonitor(player)
	end

	Players.PlayerAdded:Connect(setupTeamChangeMonitor)

	Players.PlayerRemoving:Connect(function(player)
		if teamChangeConnections[player.UserId] then
			teamChangeConnections[player.UserId]:Disconnect()
			teamChangeConnections[player.UserId] = nil
		end
	end)

	debugPrint("Takım değişikliği izleme başlatıldı")
end

-- Sadece belirli bir oyuncunun spawn noktasını değiştir
local function setupPlayerSpawn(player)
	if not player then return end

	local teamName = determineInitialTeam(player)
	local teamData = getTeamDataByName(teamName)

	if teamData then
		switchTeam(player, teamName)

		if player.Character then
			teleportToSpawn(player, teamData.spawnName)
		end
	end
end

-- Karakter ekleme olayının işlenmesi
local function handleCharacterAdded(player, character)
	local lastCharacterId = character:GetAttribute("CharacterHandled")
	if lastCharacterId then 
		debugPrint("Bu karakter zaten işlendi, atlanıyor") 
		return 
	end

	character:SetAttribute("CharacterHandled", true)
	lastHandledCharacters[player.UserId] = character

	local humanoid = character:WaitForChild("Humanoid")

	humanoid.Died:Connect(function()
		debugPrint(player.Name .. " öldü, yeniden doğma işlemi yapılacak")

		local gui = player.PlayerGui:FindFirstChild("TeamSwitcherGui")
		if gui and gui.Enabled then
			GuiAnimationService.closeGui(gui)
		end
	end)

	task.delay(0.5, function()
		if player and player.Character == character then
			teleportPlayerToTeamSpawn(player)
		end
	end)
end

-- Yeni oyuncu girdiğinde
local function onPlayerAdded(player)
	debugPrint("Yeni oyuncu girdi: " .. player.Name)

	setupPlayerGui(player)

	player.CharacterAdded:Connect(function(character)
		handleCharacterAdded(player, character)
	end)

	if player.Character then
		handleCharacterAdded(player, player.Character)
	end

	local savedTeamInfo = getPermanentTeam(player)
	if savedTeamInfo then
		switchTeam(player, savedTeamInfo.teamName)
	else
		setupPlayerSpawn(player)
	end
end

-- Oyuncu çıktığında
local function onPlayerRemoving(player)
	if not player then return end

	debugPrint("Oyuncu çıkıyor: " .. player.Name)

	playerTeamData[player.UserId] = nil
	teamSwitchCooldowns[player.UserId] = nil
	lastHandledCharacters[player.UserId] = nil
	playerPermanentTeam[player.UserId] = nil

	if teamChangeConnections[player.UserId] then
		teamChangeConnections[player.UserId]:Disconnect()
		teamChangeConnections[player.UserId] = nil
	end
end

-- Spawn Location oluşturma
local function ensureSpawnLocations()
	for _, teamData in ipairs(TEAMS) do
		if teamData.spawnName then
			findRespawnLocation(teamData.spawnName)
		end
	end

	debugPrint("Spawn konumları kontrol edildi")
end

-- Script başlangıcı
initializeTeams()
ensureSpawnLocations()
monitorTeamChanges()

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)

for _, player in pairs(Players:GetPlayers()) do
	task.spawn(function()
		onPlayerAdded(player)
	end)
end

debugPrint("TeamSwitcher Script başarıyla başlatıldı!")
Editor is loading...
Leave a Comment