CCCPaste

養成模擬器PVP英文腳本

local ReGui = loadstring(game:HttpGet('https://raw.githubusercontent.com/depthso/Dear-ReGui/refs/heads/main/ReGui.lua'))()
local Msg = loadstring(game:HttpGet('https://gist.githubusercontent.com/Tseting-nil/08653e6aa9fc12a9f097bfb10e6654e7/raw/1cb8f4efec92cb1735e85bb7d1d0761ec2dab685/%25E5%2581%25B4%25E9%2582%258A%25E9%2580%259A%25E7%259F%25A5%25E6%25A8%25A1%25E7%25B5%2584.lua'))()
local Move = loadstring(game:HttpGet('https://gist.githubusercontent.com/Tseting-nil/494a4830fa6d3466596e4e01ca25bdee/raw/4529c57d02492e9207a0a97c3e90880f8be3952d/%25E5%25B7%25A1%25E8%25B7%25AF%25E6%25A8%25A1%25E7%25B5%2584'))()
-- 路徑模組設置
Move:SetPathVisualization(false) -- 關閉路徑可視化
Move:SetJumpDisabledDistance(5) -- 設置跳躍禁用距離為 5

-- ===== --
-- 遊戲函數
local Gametable = {
	gameId = game.GameId,
	placeId = game.PlaceId,
  jobId = game.JobId,
  Stats = game:GetService("Stats"),
  MarketplaceService = game:GetService("MarketplaceService"),
  RunService = game:GetService("RunService")
}

-- ===== --
-- GUI函數
local Scripttable = {
  GUI = {
    isloading = true, -- 加載旗標
    currentRefreshId = 0, -- 當前刷新 ID
    CollapsingHeader = {},
    Textlable = {},
    Button = {},
    Boolean = {
      Switch_SafeZone_Advanced_Health = false,
      Switch_SafeZone_Advanced_Random = false,
      IS_TP_Walk_SafeZone_Advanced_Cooldown = false,
    },
    Rowkey = {
      TP_Row = {"TP Safe Zone", "TP Player","Start Follow", "Stop Follow"},
      Playerinfo_TP_Row = {"TP Player", "Goto Player", "Stop Goto"},
    }
  },
  Set_num = { 
    IS_TP_Walk_SafeZone_Advanced_Cooldown = 5, -- 傳送冷卻時間
    AutoTP_SafeZone_Advanced_Health = 30, -- 血量低於多少百分比傳送安全區
    AutoTP_SafeZone_Advanced_SafeZonemode = 1, -- 最近安全區 = 1 隨機安全區 = 2 自訂安全區 = 3
  },
  Set_boolean = {
    AutoTP_SafeZone_Advanced_Health = false, -- 血量低於設定值傳送安全區
    AutoTP_SafeZone_Advanced_Move = true, -- 模式選擇最近安全區(預設移動到安全區)
    AutoTP_SafeZone_Advanced_Last = false, -- 最近安全區
    AutoTP_SafeZone_Advanced_Random = false, -- 隨機選擇安全區
  },
  playerItems = {}, -- 玩家物品清單
}

-- ===== --
-- 功能變數
local TPcoordinate = { -- 傳送座標相關函數
	Map = {
		["Top Right Safe Zone"] = {X = 450, Y = 15, Z = - 1480},
		["Bottom Right Safe Zone"] = {X = 450, Y = 15, Z = 449},
		["Top Left Safe Zone"] = {X = - 1481, Y = 15, Z = - 1480},
		["Bottom Left Safe Zone"] = {X = - 1481, Y = 15, Z = 449},
		["Top Center Safe Zone"] = {X = - 515.5, Y = 15, Z = - 1480},
		["Bottom Center Safe Zone"] = {X = - 515.5, Y = 15, Z = 449},
		["Left Center Safe Zone"] = {X = - 1481, Y = 15, Z = - 515.5},
		["Right Center Safe Zone"] = {X = 450, Y = 15, Z = - 515.5},
		MapTP = {X = 0, Y = 0, Z = 0,}, -- 地圖傳送座標
    Mapname_Regular = "" -- 地圖傳送名稱
	},
	PlayerTP_name = "" -- 玩家傳送名稱
}
local PlayersINFO = { -- 玩家資訊
  Name = "", -- 選中的玩家名稱
  PlayerGuildName = "", -- 玩家公會
  Player_Guild_player = 0, -- 玩家公會在本伺服器的人數
  GuildPower = 0, -- 玩家公會戰力
  PlayerJob = "", -- 玩家職業
  PlayerPower = 0, -- 玩家戰力
  PlayerLevel = 0, -- 玩家等級
  PlayerHealth = 0, -- 玩家血量
  PlayerAttack = 0, -- 玩家攻擊力
  PlayerHealthPercent = 0 -- 玩家血量百分比
}
-- ===== --
-- 功能函數
local Mainfunction = {}

-- 獲取玩家血量 mode 1 = 實際血量, 2 = 百分比血量
function Mainfunction.GetPlayerHealth(playerInput, mode)
    mode = mode or 1
    local Players = game:GetService("Players")
    local player
    if typeof(playerInput) == "string" then
        player = Players:FindFirstChild(playerInput)
    else
        player = playerInput
    end
    if player and player.Character and player.Character:FindFirstChild("Humanoid") then
        local humanoid = player.Character.Humanoid
        if mode == 2 then
            return humanoid.Health / humanoid.MaxHealth * 100
        else
            return humanoid.Health
        end
    else
        return nil
    end
end

-- 傳送到座標
function Mainfunction.TP(x, y, z)
    local player = game.Players.LocalPlayer
    local character = player.Character or player.CharacterAdded:Wait()
    local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
    local targetPosition = Vector3.new(x, y + 1, z)
    humanoidRootPart.CFrame = CFrame.new(targetPosition)
end

-- 傳送到指定玩家
function Mainfunction.TPToPlayer(playerInput)
    local player
    if typeof(playerInput) == "string" then
        player = game.Players:FindFirstChild(playerInput)
    else
        player = playerInput
    end
    if player and player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
        local pos = player.Character.HumanoidRootPart.Position
        Mainfunction.TP(pos.X, pos.Y, pos.Z)
        return true
    else
        return false
    end
end

-- 統一的排序函數
function Mainfunction.sortPlayerList(items)
  -- 拆字串成 [文字,數字] 組
  local function splitAlphaNum(str)
    local parts = {}
    for text, num in str:gmatch("([^0-9]*)([0-9]*)") do
      if text ~= "" then table.insert(parts, {text:lower(), false}) end
      if num ~= "" then table.insert(parts, {tonumber(num), true}) end
    end
    return parts
  end

  -- 判斷是否以英文開頭
  local function isEnglishStart(str)
    local first = str:sub(1,1)
    return first:match("%a") ~= nil
  end

  -- 比較函數:自然排序
  local function naturalCompareParts(partsA, partsB)
    for i = 1, math.max(#partsA, #partsB) do
      local pa, pb = partsA[i], partsB[i]
      if not pa then return true end
      if not pb then return false end
      if pa[2] and pb[2] then -- 數字比大小
        if pa[1] ~= pb[1] then return pa[1] < pb[1] end
      else -- 字串比字母
        if pa[1] ~= pb[1] then return pa[1] < pb[1] end
      end
    end
    return false
  end

  -- 主排序:只看 DisplayName
  table.sort(items, function(a, b)
    local displayA = a:match("^(.-)%(") or a
    local displayB = b:match("^(.-)%(") or b

    local aEng, bEng = isEnglishStart(displayA), isEnglishStart(displayB)
    if aEng ~= bEng then
      return aEng -- 英文開頭優先
    end

    local partsA, partsB = splitAlphaNum(displayA), splitAlphaNum(displayB)
    return naturalCompareParts(partsA, partsB)
  end)
  
  return items
end

-- 獲取排序後的玩家列表
function Mainfunction.getPlayerList(hasLocalPlayer)
  local items = {}
  local localPlayer = game.Players.LocalPlayer
  for _, player in ipairs(game.Players:GetPlayers()) do
    if hasLocalPlayer then -- 包含本地玩家
      table.insert(items, player.DisplayName .. "(" .. player.Name .. ")")
    else
      -- 排除本地玩家
      if player ~= localPlayer then
        table.insert(items, player.DisplayName .. "(" .. player.Name .. ")")
      end
    end
  end
  return Mainfunction.sortPlayerList(items)
end

-- 計算當前位址最近的安全區
function Mainfunction.CalculateNearestSafeZone()
    local player = game.Players.LocalPlayer
    if not (player and player.Character and player.Character:FindFirstChild("HumanoidRootPart")) then return nil end
    local playerPos = player.Character.HumanoidRootPart.Position
    local nearestZone, minDistance = nil, math.huge
    for zoneName, coords in pairs(TPcoordinate.Map) do
        if type(coords) == "table" and coords.X and coords.Y and coords.Z then
            local zonePos = Vector3.new(coords.X, coords.Y, coords.Z)
            local distance = (playerPos - zonePos).Magnitude
            if distance < minDistance then
                minDistance = distance
                nearestZone = {name = zoneName, position = coords}
            end
        end
    end
    return nearestZone
end

-- 隨機安全區傳送(排除最近安全區)
function Mainfunction.SmartRandomSafeZoneTP()
    local player = game.Players.LocalPlayer
    if not (player and player.Character and player.Character:FindFirstChild("HumanoidRootPart")) then 
        print("Player character not ready")
        return false
    end
    
    local playerPos = player.Character.HumanoidRootPart.Position
    
    -- 定義所有有效的安全區(按順序對應 1-8)
    local allSafeZones = {
        "Top Right Safe Zone",      -- 1
        "Bottom Right Safe Zone",      -- 2
        "Top Left Safe Zone",      -- 3
        "Bottom Left Safe Zone",      -- 4
        "Top Center Safe Zone",    -- 5
        "Bottom Center Safe Zone",    -- 6
        "Left Center Safe Zone",    -- 7
        "Right Center Safe Zone"     -- 8
    }
    
    -- 找出最近的安全區索引
    local nearestZoneIndex = 3
    local minDistance = math.huge
    
    for i = 3, 10 do  -- 只在實際安全區中查找(跳過前兩個標題項)
        local zoneName = allSafeZones[i]
        local coords = TPcoordinate.Map[zoneName]
        if coords and coords.X and coords.Y and coords.Z then
            local zonePos = Vector3.new(coords.X, coords.Y, coords.Z)
            local distance = (playerPos - zonePos).Magnitude
            if distance < minDistance then
                minDistance = distance
                nearestZoneIndex = i
            end
        end
    end
    
    -- 建立可選索引清單(1-8,排除最近的)
    local availableIndexes = {}
    for i = 3, 10 do  -- 從索引 3 開始,對應實際的安全區(跳過"最近安全區"和"隨機安全區")
        if i ~= nearestZoneIndex then
            table.insert(availableIndexes, i)
        end
    end
    
    -- 如果沒有可選項(理論上不會發生),使用所有索引
    if #availableIndexes == 0 then
        availableIndexes = {3, 4, 5, 6, 7, 8, 9, 10}
    end
    
    -- 隨機選擇一個索引(實際安全區中的一個,已排除最近的)
    local randomIndex = availableIndexes[math.random(1, #availableIndexes)]
    local selectedZoneName = allSafeZones[randomIndex]
    local coords = TPcoordinate.Map[selectedZoneName]
    
    if coords and coords.X and coords.Y and coords.Z then
        -- 執行傳送
        Mainfunction.TP(coords.X, coords.Y, coords.Z)
        
        local nearestZoneName = allSafeZones[nearestZoneIndex]
        print("Random TP Safe Zone: " .. selectedZoneName .. " (excluded nearest: " .. nearestZoneName .. ")")
        
        return true, selectedZoneName
    else
        print("Selected safe zone coordinates are invalid")
        return false
    end
end
function Mainfunction.FormatValue(value)
  local formatted = ""
  if value >= 1e16 then        -- 10,000 T = 1e16
    formatted = string.format("%.2f", value / 1e15) .. "Qa"
  elseif value >= 1e12 then    -- T
    formatted = string.format("%.2f", value / 1e12) .. "T"
  elseif value >= 1e9 then     -- B
    formatted = string.format("%.2f", value / 1e9) .. "B"
  elseif value >= 1e6 then     -- M
    formatted = string.format("%.2f", value / 1e6) .. "M"
  elseif value >= 1e3 then     -- K
    formatted = string.format("%.2f", value / 1e3) .. "K"
  else
    formatted = tostring(value)
  end
  -- 去掉多餘的 .00 或結尾 0
  formatted = formatted:gsub("%.00$", "")
  formatted = formatted:gsub("(%..-)0$", "%1")
  return formatted
end
-- ====== --
-- 主視窗建立、標籤
local Window = ReGui:TabsWindow({
	Title = "PVP",
	Size = UDim2.fromOffset(420, 300), -- (寬, 高)
})

-- 頁籤
local features1 = Window:CreateTab({
	Name = "Main",
	TextSize = 18
})
local features2 = Window:CreateTab({
	Name = "TP_Options",
	TextSize = 18
})
local features3 = Window:CreateTab({
	Name = "Player Info",
	TextSize = 18
})
local features4 = Window:CreateTab({
  Name = "Server Info",
  TextSize = 18
})
local Content = features1:ScrollingCanvas({
	Fill = true,
	UiPadding = UDim.new(0, 0)
})
local TP_Content = features2:ScrollingCanvas({
	Fill = true,
	UiPadding = UDim.new(0, 0)
})
local Player_Content = features3:ScrollingCanvas({
	Fill = true,
	UiPadding = UDim.new(0, 0)
})
local Server_Content = features4:ScrollingCanvas({
	Fill = true,
	UiPadding = UDim.new(0, 0)
})
spawn(function()
	task.wait(0.1)
	local features1_Button = features1.TabButton.Button
	local features2_Button = features2.TabButton.Button
	local features3_Button = features3.TabButton.Button
	local features4_Button = features4.TabButton.Button
	local features1_label = features1_Button:FindFirstChildOfClass("TextLabel")
	local features2_label = features2_Button:FindFirstChildOfClass("TextLabel")
	local features3_label = features3_Button:FindFirstChildOfClass("TextLabel")
	local features4_label = features4_Button:FindFirstChildOfClass("TextLabel")
  features1_label.TextSize = 18
  features2_label.TextSize = 18
  features3_label.TextSize = 18
  features4_label.TextSize = 18
end)

-- ===== --
-- 第一頁籤頁
Content:Button({
	Text = "Equipment Script",
  TextSize = 18,
	Callback = function(self)
		loadstring(game:HttpGet('https://pastebin.com/raw/Dphgu3Kq'))()
	end
})

Content:Button({
	Text = "PVP Script (Multi-Window)",
  TextSize = 18,
	Callback = function(self)
		loadstring(game:HttpGet('https://pastebin.com/raw/QUGqp0E3'))()
	end
})

-- ===== --
-- 第二頁籤頁
Scripttable.GUI.Textlable.TPSafeZone_label = TP_Content:Label({
	Text = "SafeZone: No selected",
	TextSize = 18,
})

Scripttable.GUI.Textlable.TPPlayer_label = TP_Content:Label({
	Text = "Player : No player selected",
	TextSize = 18,
})
TP_Content:Separator({
	Text = "● Teleport Options",
  TextColor3 = Color3.fromRGB(255, 255, 0)
})
Scripttable.GUI.Button.ButtonsRow = TP_Content:Row()

-- 使用循環創建按鈕
for key, value in pairs(Scripttable.GUI.Rowkey.TP_Row) do
	Scripttable.GUI.Button.ButtonsRow:Button({
		Text = value,
		TextSize = 16,
		Callback = function()
      local ISfollow = false
      if Scripttable.GUI.isloading then
        return
      end

      if value == "Start Follow" then
        if TPcoordinate.PlayerTP_name == "" then
          Scripttable.GUI.Textlable.TPPlayer_label.Text = "Player : No follow player selected"
          Msg:Warning("No player selected", 3)
          return
        end
      elseif value == "TP Safe Zone" then
        if TPcoordinate.Map.MapTP.X == 0 and TPcoordinate.Map.MapTP.Y == 0 and TPcoordinate.Map.MapTP.Z == 0 then
          Scripttable.GUI.Textlable.TPSafeZone_label.Text = "SafeZone : Not slected"
          Msg:Warning("SafeZone : Not slected", 3)
          return
        end
      elseif value == "TP Player" then
        if TPcoordinate.PlayerTP_name == "" then
          Scripttable.GUI.Textlable.TPPlayer_label.Text = "Player : No player selected"
          Msg:Warning("No player selected", 3)
          return
        end
      end

			if value == "TP Safe Zone" then
        print("TP Safe Zone")
				local pos = TPcoordinate.Map.MapTP
				if pos.X ~= 0 and pos.Y ~= 0 and pos.Z ~= 0 then
					Mainfunction.TP(pos.X, pos.Y, pos.Z)
				end
			elseif value == "TP Player" then
				if TPcoordinate.PlayerTP_name ~= "" then
					if game.Players:FindFirstChild(TPcoordinate.PlayerTP_name) then
						Mainfunction.TPToPlayer(TPcoordinate.PlayerTP_name)
						Scripttable.GUI.Textlable.TPPlayer_label.Text = "Teleport player: " .. TPcoordinate.PlayerTP_name
					else
						Scripttable.GUI.Textlable.TPPlayer_label.Text = "Selected player: (Offline) " .. TPcoordinate.PlayerTP_name
					end
				else
					Scripttable.GUI.Textlable.TPPlayer_label.Text = "Player : No player selected"
          Msg:Show("Player : No player selected", 3)
				end
      elseif value == "Start Follow" then
        if TPcoordinate.PlayerTP_name == "" then
          Scripttable.GUI.Textlable.TPPlayer_label.Text = "Player : No player selected"
          Msg:Warning("No follow player selected", 3)
          return
        end
        if not game.Players:FindFirstChild(TPcoordinate.PlayerTP_name) then
          Scripttable.GUI.Textlable.TPPlayer_label.Text = "Selected player: (Offline) " .. TPcoordinate.PlayerTP_name
          Msg:Warning("Selected player: (Offline) " .. TPcoordinate.PlayerTP_name, 3)
          return
        end
        if ISfollow then
          Msg:Warning("Already following: " .. TPcoordinate.PlayerTP_name, 3)
          return
        else
          ISfollow = true
        end
        Scripttable.GUI.Textlable.TPPlayer_label.Text = "Following: " .. TPcoordinate.PlayerTP_name
        Msg:Success("Following: " .. TPcoordinate.PlayerTP_name, 3)
        Move:NavigateToPlayer(TPcoordinate.PlayerTP_name, true)
			elseif value == "Stop Follow" then
        ISfollow = false
        Scripttable.GUI.Textlable.TPPlayer_label.Text = "Stopped following: " .. TPcoordinate.PlayerTP_name
        Msg:Info("Stopped following: " .. TPcoordinate.PlayerTP_name, 3)
        Move:StopPlayerTracking()
      end
		end
	})
end
TP_Content:Separator({
	Text = "● Function Options",
	TextColor3 = Color3.fromRGB(255, 255, 0)
})
Scripttable.GUI.Button.ButtonsRow_last = TP_Content:Row()
-- 添加最近傳送按鈕
Scripttable.GUI.Button.ButtonsRow_last:Button({
  Text = "TP Nearest Safe Zone",
  TextSize = 16,
  Callback = function()
    local nearestZone = Mainfunction.CalculateNearestSafeZone()
    if nearestZone and nearestZone.position then
      -- 執行傳送
      Mainfunction.TP(nearestZone.position.X, nearestZone.position.Y, nearestZone.position.Z)
      Msg:Success("TP Nearest Safe Zone: " .. nearestZone.name, 4)
      print("TP Nearest Safe Zone " .. nearestZone.name)
    end
  end
})

-- 添加隨機傳送按鈕
Scripttable.GUI.Button.ButtonsRow_last:Button({
  Text = "TP random Safe Zone",
  TextSize = 16,
  Callback = function()
    local success, zoneName = Mainfunction.SmartRandomSafeZoneTP()
    if success then
      Msg:Success("TP random Safe Zone: " .. zoneName, 4)
      print("TP random Safe Zone: " .. zoneName)
    end
  end
})
TP_Content:Combo({
	Label = "Select Safe Zone",
	Items = {
		"Top Right Safe Zone",
		"Bottom Right Safe Zone",
		"Top Left Safe Zone",
		"Bottom Left Safe Zone",
		"Top Center Safe Zone",
		"Bottom Center Safe Zone",
		"Left Center Safe Zone",
		"Right Center Safe Zone"
	},
	Callback = function(Item)
        -- 假設 UI 庫回傳 {Value="xxx"},就取 Item.Value
		local key = Item.Value or Item
		local coord = TPcoordinate.Map[key]
		if coord then
			TPcoordinate.Map.MapTP.X = coord.X
			TPcoordinate.Map.MapTP.Y = coord.Y
			TPcoordinate.Map.MapTP.Z = coord.Z
			Scripttable.GUI.Textlable.TPSafeZone_label.Text = "Selected teleport area: " .. key
			TPcoordinate.Map.Mapname_Regular = tostring(key)
			print("Selected teleport area: " .. key .. " coordinates: X=" .. coord.X .. ", Y=" .. coord.Y .. ", Z=" .. coord.Z)
		else
			Scripttable.GUI.Textlable.TPSafeZone_label.Text = "Coordinates not found: " .. tostring(Item)
		end
	end
})

-- 建立下拉
Scripttable.GUI.CollapsingHeader.TP_Content_Playerlist = TP_Content:Combo({
	Label = "Player List",
	GetItems = function()
		-- 使用統一的排序邏輯
		return Mainfunction.getPlayerList(false) -- 不包含本地玩家
	end,
	Callback = function(selected)
		local text = (type(selected) == "table" and (selected.Value or selected.Label)) or tostring(selected)
		local username = text:match("%((.+)%)")
		local player = username and game.Players:FindFirstChild(username)
		if player then
			TPcoordinate.PlayerTP_name = username
			Scripttable.GUI.Textlable.TPPlayer_label.Text = "Selected teleport player: " .. username
			print("Selected teleport player: " .. TPcoordinate.PlayerTP_name)
		else
			TPcoordinate.PlayerTP_name = ""
			Scripttable.GUI.Textlable.TPPlayer_label.Text = "Selected player: (Offline) " .. username
			Msg:Warning("Selected player: (Offline) " .. username, 5)
			print("Selected player: (Offline) " .. username)
		end
	end,
})

-- 安全區進階設置
Scripttable.GUI.CollapsingHeader.TP_Content_SafeZone_Advanced = TP_Content:CollapsingHeader({
  Title = "Advanced Safe Zone Settings",
  TextSize = 18
})
Scripttable.GUI.CollapsingHeader.TP_Content_SafeZone_Advanced_Healthtext = Scripttable.GUI.CollapsingHeader.TP_Content_SafeZone_Advanced:Label({
  Text = "Health below: 30 % (Default value)",
  TextSize = 14,
})
Scripttable.GUI.CollapsingHeader.TP_Content_SafeZone_Advanced_Healthswitch = Scripttable.GUI.CollapsingHeader.TP_Content_SafeZone_Advanced:Radiobox({
  Value = false,
  Label = "TP Safe Zone when health is below set value",
  Callback = function(self, Value: boolean)
    if Scripttable.GUI.isloading then
      return
    end
    if Value ~= true then
      Scripttable.Set_boolean.AutoTP_SafeZone_Advanced_Health = false
      return
    end
    local ModalWindow = Window:PopupModal({Title = "TP Safe Zone when health is below set value"})
    ModalWindow:Label({
      Text = "Please enter a number (1% - 99%)",
      TextSize = 16,
    })
    ModalWindow:InputInt({
      Label = "(%)",
      Value = Scripttable.Set_num.AutoTP_SafeZone_Advanced_Health,
      Maximum = 99,
      Minimum = 1,
      Callback = function(InputObj)
        -- 直接更新顯示文字,而不需要額外變數
        Scripttable.GUI.CollapsingHeader.TP_Content_SafeZone_Advanced_Healthtext.Text = "Health below: " .. tostring(InputObj.Value) .. " %"
        Scripttable.Set_num.AutoTP_SafeZone_Advanced_Health = InputObj.Value
      end,
    })

    local Row = ModalWindow:Row({Expanded = true})

    Row:Button({
      Text = "Confirm",
      Callback = function()
        Scripttable.Set_boolean.AutoTP_SafeZone_Advanced_Health = true
        ModalWindow:ClosePopup()
        if Scripttable.Set_num.AutoTP_SafeZone_Advanced_Health then
          task.spawn(function()
            while Scripttable.Set_boolean.AutoTP_SafeZone_Advanced_Health do
              task.wait(1)
              local character = game.Players.LocalPlayer.Character
              local humanoid = character and character:FindFirstChild("Humanoid")
              if humanoid then
                local healthPercent = humanoid.Health / humanoid.MaxHealth * 100
                if healthPercent < Scripttable.Set_num.AutoTP_SafeZone_Advanced_Health then
                  if not Scripttable.Set_boolean.IS_TP_Walk_SafeZone_Advanced_Cooldown then
                      --print("Health below set value, TP Safe Zone")
                    if Scripttable.Set_num.AutoTP_SafeZone_Advanced_SafeZonemode == 1 then
                        -- 最近安全區
                      local nearestZone = Mainfunction.CalculateNearestSafeZone()
                      if nearestZone and nearestZone.position and not Scripttable.Set_boolean.AutoTP_SafeZone_Advanced_Move then
                        Mainfunction.TP(nearestZone.position.X, nearestZone.position.Y, nearestZone.position.Z)
                        print("Teleported to nearest safe zone: " .. nearestZone.name)
                        Msg:Info("Teleported to nearest safe zone: " .. nearestZone.name, 3)
                        Move:PausePlayerTracking()
                        task.wait(5)
                        Move:ResumePlayerTracking()
                      elseif nearestZone and nearestZone.position and Scripttable.Set_boolean.AutoTP_SafeZone_Advanced_Move then
                        Move:PausePlayerTracking()
                        Move:NavigateToPosition(nearestZone.position.X, nearestZone.position.Y, nearestZone.position.Z)
                        print("Moved to nearest safe zone: " .. nearestZone.name)
                        Msg:Info("Moved to nearest safe zone: " .. nearestZone.name, 3)
                        task.wait(5)
                        Move:ResumePlayerTracking()
                      else
                        print("Cannot find nearest safe zone")
                      end
                    elseif Scripttable.Set_num.AutoTP_SafeZone_Advanced_SafeZonemode == 2 then
                        -- 隨機安全區(排除最近安全區)
                      local success, zoneName = Mainfunction.SmartRandomSafeZoneTP()
                      if success then
                        print("Health too low, randomly teleported to safe zone: " .. zoneName)
                        Msg:Info("Health too low, teleported to: " .. zoneName, 3)
                      else
                        print("Random teleport failed")
                        Msg:Warning("Random teleport failed", 3)
                      end
                    elseif Scripttable.Set_num.AutoTP_SafeZone_Advanced_SafeZonemode == 3 then
                        -- 自訂安全區
                      local pos = TPcoordinate.Map.MapTP
                      if pos.X ~= 0 and pos.Y ~= 0 and pos.Z ~= 0 then
                        Mainfunction.TP(pos.X, pos.Y, pos.Z)
                        print("Teleported to custom safe zone: " .. TPcoordinate.Map.Mapname_Regular)
                        Msg:Info("Teleported to custom safe zone: " .. TPcoordinate.Map.Mapname_Regular, 3)
                      else
                        print("Invalid custom area, please select again")
                        Msg:Warning("Invalid custom area, please select again", 3)
                      end
                    end
                      -- 傳送後等待冷卻時間
                    Scripttable.Set_boolean.IS_TP_Walk_SafeZone_Advanced_Cooldown = true
                    spawn(function()
                      task.wait(Scripttable.Set_num.IS_TP_Walk_SafeZone_Advanced_Cooldown)
                      Scripttable.Set_boolean.IS_TP_Walk_SafeZone_Advanced_Cooldown = false
                    end)
                  else
                    print("Teleport on cooldown, cannot teleport again")
                  end
                end
              end
            end
          end)
        end
      end,
    })

    Row:Button({
      Text = "Cancel",
      Callback = function()
        Scripttable.Set_boolean.AutoTP_SafeZone_Advanced_Health = false
        Scripttable.GUI.CollapsingHeader.TP_Content_SafeZone_Advanced_Healthswitch:SetValue(false)
        ModalWindow:ClosePopup()
      end,
    })
    print("TP Safe Zone when health is below set value: ", Value)
  end
})

-- 建立安全區選擇
local SafeZoneCombo = Scripttable.GUI.CollapsingHeader.TP_Content_SafeZone_Advanced:Combo({
  Label = "Safe Zone Selection",
  Placeholder = "Nearest Safe Zone",
  Items = {
    "Nearest Safe Zone",
    "Random Safe Zone",
    "Custom Safe Zone"
  },
  Callback = function(Combo, Value: any)
    if Scripttable.GUI.isloading then return end

    if Value == "Nearest Safe Zone" then
      Scripttable.Set_num.AutoTP_SafeZone_Advanced_SafeZonemode = 1
      print("Safe Zone Selection: Nearest Safe Zone")
      Scripttable.GUI.CollapsingHeader.TP_Content_SafeZone_Advanced_MoveMode.Visible = true  -- 顯示移動模式
      Scripttable.GUI.CollapsingHeader.TP_Content_SafeZone_Advanced_RandomButton.Visible = true -- 顯示隨機傳送按鈕
    elseif Value == "Random Safe Zone" then
      Scripttable.Set_num.AutoTP_SafeZone_Advanced_SafeZonemode = 2
      print("Safe Zone Selection: Random Safe Zone (automatically excludes nearest)")
      Scripttable.GUI.CollapsingHeader.TP_Content_SafeZone_Advanced_MoveMode.Visible = false -- 隱藏移動模式
      Scripttable.GUI.CollapsingHeader.TP_Content_SafeZone_Advanced_RandomButton.Visible = false -- 隱藏隨機傳送按鈕
      Msg:Info("Random mode: Automatically excludes nearest safe zone to avoid tracking", 4)
    elseif Value == "Custom Safe Zone" then
      if TPcoordinate.Map.Mapname_Regular ~= "" then
        Scripttable.Set_num.AutoTP_SafeZone_Advanced_SafeZonemode = 3
        print("Safe Zone Selection: Custom Safe Zone")
      else
        print("Safe Zone Selection: Custom Safe Zone (invalid, changed to nearest)")
        Msg:Warning("Please select a teleport safe zone first", 5)
      end
      Scripttable.GUI.CollapsingHeader.TP_Content_SafeZone_Advanced_MoveMode.Visible = false -- 隱藏移動模式
      Scripttable.GUI.CollapsingHeader.TP_Content_SafeZone_Advanced_RandomButton.Visible = true -- 顯示隨機傳送按鈕
    end
  end
})

-- 建立移動模式選擇,預設隱藏
Scripttable.GUI.CollapsingHeader.TP_Content_SafeZone_Advanced_MoveMode = Scripttable.GUI.CollapsingHeader.TP_Content_SafeZone_Advanced:Combo({
  Label = "Movement Mode Selection",
  Placeholder = "Move to Safe Zone",
  Items = {
    "Move to Safe Zone",
    "TP Safe Zone",
  },
  Callback = function(Combo, Value: any)
    if Scripttable.GUI.isloading then return end

    if Value == "Move to Safe Zone" then
      Scripttable.Set_boolean.AutoTP_SafeZone_Advanced_Move = true
      print("Movement Mode Selection: Move to Safe Zone")
    elseif Value == "TP Safe Zone" then
      Msg:Warning("TP Safe Zone, risk at your own responsibility", 5)
      Scripttable.Set_boolean.AutoTP_SafeZone_Advanced_Move = false
      print("Movement Mode Selection: TP Safe Zone")
    end
  end
})

-- 添加隨機傳送測試按鈕
Scripttable.GUI.CollapsingHeader.TP_Content_SafeZone_Advanced_RandomButton = Scripttable.GUI.CollapsingHeader.TP_Content_SafeZone_Advanced:Button({
  Text = "Random TP Safe Zone",
  TextSize = 14,
  Callback = function()
    local success, zoneName = Mainfunction.SmartRandomSafeZoneTP()
    if success then
      Msg:Success("Test successful! Teleported to: " .. zoneName, 4)
    else
      Msg:Warning("Test failed! Please check settings", 3)
    end
  end
})
-- ===== --
-- 第三頁籤頁
local function ResetPlayerInfo()
  PlayersINFO.Name = ""
  PlayersINFO.PlayerGuildName = "None"
  PlayersINFO.Player_Guild_player = 0
  PlayersINFO.GuildPower = 0
  PlayersINFO.PlayerPower = 0
  PlayersINFO.PlayerLevel = 0
  PlayersINFO.PlayerHealth = 0
  PlayersINFO.PlayerHealthPercent = 0
end

Scripttable.GUI.CollapsingHeader.Player_Content_Playerlist = Player_Content:Combo({
  Label = "Player List",
  GetItems = function()
    return Mainfunction.getPlayerList(true) -- 包含本地玩家
  end,
  Callback = function(selected)
    ResetPlayerInfo()
    if Scripttable.GUI.isloading then return end

    local text = (type(selected) == "table" and (selected.Value or selected.Label)) or tostring(selected)
    local username = text:match("%((.+)%)")
    local player = username and game.Players:FindFirstChild(username)

    if not player then
      ResetPlayerInfo()
      Scripttable.GUI.CollapsingHeader.Player_Content_Playername.Text = "Selected Player: (Offline) " .. (username or "")
      Msg:Warning("Selected Player: (Offline) " .. (username or ""), 5)
      print("Selected Player: (Offline) " .. (username or ""))
      return
    end

    -- 更新全域選擇
    PlayersINFO.Name = username
    print("Selected teleport player: " .. PlayersINFO.Name)

    -- 切換玩家時增加 refreshId,舊的迴圈會自動失效
    Scripttable.GUI.currentRefreshId = Scripttable.GUI.currentRefreshId + 1
    local myRefreshId = Scripttable.GUI.currentRefreshId

    task.spawn(function()
      while PlayersINFO.Name ~= "" and myRefreshId == Scripttable.GUI.currentRefreshId do
        local targetPlayer = game.Players:FindFirstChild(PlayersINFO.Name)
        if not targetPlayer then
          if myRefreshId == Scripttable.GUI.currentRefreshId then
            ResetPlayerInfo()
            Scripttable.GUI.CollapsingHeader.Player_Content_Playername.Text = "Selected Player: (Offline) " .. username
            Msg:Warning("Selected Player: (Offline) " .. username, 5)
            print("Selected Player: (Offline) " .. username)
          end
          break
        end

        local playerInfo = targetPlayer:FindFirstChild("值") and targetPlayer:WaitForChild("值"):WaitForChild("信息")
        if playerInfo then
          PlayersINFO.PlayerGuildName = playerInfo:FindFirstChild("公会名称") and playerInfo:WaitForChild("公会名称").Value or ""
          PlayersINFO.GuildPower = playerInfo:FindFirstChild("公会战斗力") and playerInfo:WaitForChild("公会战斗力").Value or 0
          if PlayersINFO.PlayerGuildName == "" then
            PlayersINFO.PlayerGuildName = "No Guild"
            PlayersINFO.GuildPower = 0
          end
        end

        local guildCount = 1
        if PlayersINFO.PlayerGuildName ~= "" then
          if PlayersINFO.PlayerGuildName ~= "No Guild" then
            for _, p in pairs(game.Players:GetPlayers()) do
            if p ~= targetPlayer then
              local pInfo = p:FindFirstChild("值") and p:WaitForChild("值"):FindFirstChild("信息")
              if pInfo then
                local pGuildName = pInfo:FindFirstChild("公会名称") and pInfo:WaitForChild("公会名称").Value or ""
                if pGuildName == PlayersINFO.PlayerGuildName then
                  guildCount = guildCount + 1
                end
              end
            end
            end
          else
            guildCount = 0
          end
        end

        local level = targetPlayer:WaitForChild("值"):FindFirstChild("信息"):FindFirstChild("等级").Value
        PlayersINFO.PlayerLevel = level

        local job = targetPlayer:WaitForChild("值"):FindFirstChild("信息"):FindFirstChild("职业").Value
        job = tostring(job)
        local jobMap = {
          ["1"] = "Novice",
          ["2"] = "Swordsman (Attack)",
          ["3"] = "Defender (Shield)",
          ["4"] = "Soul Master (Support)",
          ["5"] = "Healer (Heal)",
          ["6"] = "Sword Master (Attack)",
          ["7"] = "Guardian Veteran (Shield)",
          ["8"] = "Spirit Adept (Support)",
          ["9"] = "Holy Hand (Heal)",
          ["10"] = "Sword Monarch (Attack)",
          ["11"] = "Fortress (Shield)",
          ["12"] = "Soul Archmage (Support)",
          ["13"] = "Warden (Heal)",
          ["14"] = "Sword Deity (Attack)",
          ["15"] = "Immortal Defender (Shield)",
          ["16"] = "Divine Spirit Lord (Support)",
          ["17"] = "Eternal Restoter (Heal)",
        }
        PlayersINFO.PlayerJob = jobMap[job] or "Unknown"

        local power = targetPlayer:WaitForChild("值"):FindFirstChild("最终值"):FindFirstChild("战斗力").Value
        PlayersINFO.PlayerPower = power

        local attack = targetPlayer:WaitForChild("值"):FindFirstChild("最终值"):FindFirstChild("攻击力").Value
        PlayersINFO.PlayerAttack = attack

        if targetPlayer.Character and targetPlayer.Character:FindFirstChild("Humanoid") then
          local humanoid = targetPlayer.Character.Humanoid
          PlayersINFO.PlayerHealth = math.floor(humanoid.Health)
          PlayersINFO.PlayerHealthPercent = math.floor((humanoid.Health / humanoid.MaxHealth) * 100)
        else
          PlayersINFO.PlayerHealth = 0
          PlayersINFO.PlayerHealthPercent = 0
        end

        -- 更新 UI
        Scripttable.GUI.CollapsingHeader.Player_Content_Playername.Text = "Selected Player: " .. text
        Scripttable.GUI.CollapsingHeader.Player_Content_PlayerInfo_PlayerGuild.Text = "Guild Name: " .. (PlayersINFO.PlayerGuildName ~= "" and PlayersINFO.PlayerGuildName or "None")
        Scripttable.GUI.CollapsingHeader.Player_Content_PlayerInfo_GuildPower.Text = "Members on this server: " .. tostring(guildCount) .. " , Guild Power: " .. Mainfunction.FormatValue(PlayersINFO.GuildPower)
        Scripttable.GUI.CollapsingHeader.Player_Content_PlayerInfo_PlayerLevel.Text = "Level: " .. tostring(PlayersINFO.PlayerLevel) .. " , Class: " .. (PlayersINFO.PlayerJob ~= "" and PlayersINFO.PlayerJob or "None")
        Scripttable.GUI.CollapsingHeader.Player_Content_PlayerInfo_PlayerPower.Text = "Power: " .. Mainfunction.FormatValue(PlayersINFO.PlayerPower)
        Scripttable.GUI.CollapsingHeader.Player_Content_PlayerInfo_PlayerHealth.Text = "Health: " .. Mainfunction.FormatValue(PlayersINFO.PlayerHealth) .. " , " .. tostring(PlayersINFO.PlayerHealthPercent) .. " %"
        Scripttable.GUI.CollapsingHeader.Player_Content_PlayerInfo_PlayerAttack.Text = "Attack: " .. Mainfunction.FormatValue(PlayersINFO.PlayerAttack)

        task.wait(0.1)
      end
    end)
  end,
})

Scripttable.GUI.CollapsingHeader.Player_Content_Playername = Player_Content:Label({
    Text = "Name: No player selected",
    TextSize = 16,
})
Scripttable.GUI.CollapsingHeader.Player_Content_PlayerInfo_PlayerGuild = Player_Content:Label({
    Text = "Guild Name: None",
    TextSize = 16,
})
Scripttable.GUI.CollapsingHeader.Player_Content_PlayerInfo_GuildPower = Player_Content:Label({
    Text = "Members on this server: 0 , Guild Power: 0",
    TextSize = 16,
})
Scripttable.GUI.CollapsingHeader.Player_Content_PlayerInfo_PlayerLevel = Player_Content:Label({
    Text = "Level: 0 , Class: None",
    TextSize = 16,
})
Scripttable.GUI.CollapsingHeader.Player_Content_PlayerInfo_PlayerPower = Player_Content:Label({
    Text = "Power: 0",
    TextSize = 16,
})
Scripttable.GUI.CollapsingHeader.Player_Content_PlayerInfo_PlayerHealth = Player_Content:Label({
    Text = "Health: 0 , Percentage: 0 %",
    TextSize = 16,
})
Scripttable.GUI.CollapsingHeader.Player_Content_PlayerInfo_PlayerAttack = Player_Content:Label({
    Text = "Attack: 0 ",
    TextSize = 16,
})

Player_Content:Separator({Text="● Function Options"})
Scripttable.GUI.Button.ButtonsRow_2 = Player_Content:Row()

-- 使用循環創建按鈕
for key, value in pairs(Scripttable.GUI.Rowkey.Playerinfo_TP_Row) do
	Scripttable.GUI.Button.ButtonsRow_2:Button({
		Text = value,
		TextSize = 16,
		Callback = function()
      if Scripttable.GUI.isloading then
        return
      end
      if PlayersINFO.Name == "" then
        Scripttable.GUI.CollapsingHeader.Player_Content_Playername.Text = "No follow player selected"
        Msg:Warning("No player selected", 3)
        return
      end
      if PlayersINFO.Name == game.Players.LocalPlayer.Name then -- 排除自己
        Scripttable.GUI.CollapsingHeader.Player_Content_Playername.Text = "Cannot teleport/move to yourself"
        Msg:Warning("Cannot teleport/move to yourself", 3)
        return
      end

			if value == "TP Player" then
        if PlayersINFO.Name ~= "" then
          if game.Players:FindFirstChild(PlayersINFO.Name) then
            Mainfunction.TPToPlayer(PlayersINFO.Name)
            Scripttable.GUI.CollapsingHeader.Player_Content_Playername.Text = "Selected teleport player: " .. PlayersINFO.Name
          else
            Scripttable.GUI.CollapsingHeader.Player_Content_Playername.Text = "Selected player: (Offline) " .. PlayersINFO.Name
            Msg:Warning("Selected player: (Offline) " .. PlayersINFO.Name, 3)
          end
        else
          Msg:Warning("Player : No player selected", 3)
        end
      elseif value == "Goto Player" then
        if PlayersINFO.Name ~= "" then
          if game.Players:FindFirstChild(PlayersINFO.Name) then
            Move:NavigateToPlayer(PlayersINFO.Name, false)
            Scripttable.GUI.CollapsingHeader.Player_Content_Playername.Text = "Moving to: " .. PlayersINFO.Name
            Msg:Success("Moving to: " .. PlayersINFO.Name, 3)
          else
            Scripttable.GUI.CollapsingHeader.Player_Content_Playername.Text = "Selected player: (Offline) " .. PlayersINFO.Name
            Msg:Warning("Selected player: (Offline) " .. PlayersINFO.Name, 3)
          end
        else
          Msg:Warning("No movement player selected", 3)
        end
      elseif value == "Stop Goto" then
        Move:StopPlayerTracking() -- 停止移動
        Scripttable.GUI.CollapsingHeader.Player_Content_Playername.Text = "Stopped movement: " .. PlayersINFO.Name
        Msg:Info("Stopped movement: " .. PlayersINFO.Name, 3)
      end
		end
	})
end

-- ===== --
-- 第四頁籤頁
-- FPS 計算
local fps = 0
local lastUpdate = tick()
Gametable.RunService.RenderStepped:Connect(function()
    local now = tick()
    fps = 1 / (now - lastUpdate)
    lastUpdate = now
end)

-- 自動更新函式
local function UpdateServerInfo()
    -- 玩家數
    Scripttable.GUI.CollapsingHeader.Server_Content_PlayerCount.Text = "Server Players: " .. tostring(#game.Players:GetPlayers()) .. " / " .. tostring(game.Players.MaxPlayers)
    
    -- 運行時間
    Scripttable.GUI.CollapsingHeader.Server_Content_Uptime.Text = "Server Uptime: " .. math.floor(workspace.DistributedGameTime) .. " seconds"

    -- Ping
    Scripttable.GUI.CollapsingHeader.Server_Content_Ping.Text = "Ping: " .. Gametable.Stats.Network.ServerStatsItem["Data Ping"]:GetValueString()

    -- FPS
    Scripttable.GUI.CollapsingHeader.Server_Content_FPS.Text = "FPS: " .. tostring(math.floor(fps))
end

Scripttable.GUI.CollapsingHeader.Server_Content_GameName = Server_Content:Label({
  Text = "Game Name: " .. Gametable.MarketplaceService:GetProductInfo(game.PlaceId).Name,
  TextSize = 16,
})
Scripttable.GUI.CollapsingHeader.Server_Content_GameID = Server_Content:Label({
  Text = "Game ID: " .. tostring(game.GameId),
  TextSize = 16,
})
Scripttable.GUI.CollapsingHeader.Server_Content_MapID = Server_Content:Label({
  Text = "Map ID: " .. tostring(game.PlaceId),
  TextSize = 16,
})
Scripttable.GUI.CollapsingHeader.Server_Content_JobID = Server_Content:Label({
  Text = "Server ID: " .. game.JobId,
  TextSize = 16,
})
Scripttable.GUI.CollapsingHeader.Server_Content_PlayerCount = Server_Content:Label({
  Text = "Server Players: " .. tostring(#game.Players:GetPlayers()) .. " / " .. tostring(game.Players.MaxPlayers),
  TextSize = 16,
})
Scripttable.GUI.CollapsingHeader.Server_Content_Uptime = Server_Content:Label({
  Text = "Server Uptime: " .. math.floor(workspace.DistributedGameTime) .. " seconds",
  TextSize = 16,
})
Scripttable.GUI.CollapsingHeader.Server_Content_Ping = Server_Content:Label({
  Text = "Ping: " .. Gametable.Stats.Network.ServerStatsItem["Data Ping"]:GetValueString(),
  TextSize = 16,
})
Scripttable.GUI.CollapsingHeader.Server_Content_FPS = Server_Content:Label({
  Text = "FPS: 0" ,
  TextSize = 16,
})

-- ===== --
-- 綁定事件
spawn(function()
  task.wait(0.1)
  Scripttable.GUI.isloading = false
  while task.wait(1) do
        UpdateServerInfo()
    end
end)