local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

local player = Players.LocalPlayer

local waypoints = {}
local selectedWaypoint = nil
local selectedButton = nil
local movementMethod = "Tween"
local minimized = false

-- ScreenGui
local gui = Instance.new("ScreenGui")
gui.Name = "WaypointSystem"
gui.ResetOnSpawn = false
gui.Parent = player:WaitForChild("PlayerGui")

-- Main frame
local frame = Instance.new("Frame")
frame.Size = UDim2.new(0,260,0,340)
frame.Position = UDim2.new(0.5,-130,0.6,-170)
frame.BackgroundColor3 = Color3.fromRGB(30,30,30)
frame.BorderSizePixel = 0
frame.Active = true
frame.Draggable = true
frame.Parent = gui

-- Title bar
local titleBar = Instance.new("Frame")
titleBar.Size = UDim2.new(1,0,0,28)
titleBar.BackgroundColor3 = Color3.fromRGB(25,25,25)
titleBar.Parent = frame

local title = Instance.new("TextLabel")
title.Size = UDim2.new(1,-60,1,0)
title.Position = UDim2.new(0,6,0,0)
title.BackgroundTransparency = 1
title.Text = "Waypoints"
title.Font = Enum.Font.Code
title.TextSize = 17
title.TextXAlignment = Enum.TextXAlignment.Left
title.TextColor3 = Color3.new(1,1,1)
title.Parent = titleBar

local minimizeBtn = Instance.new("TextButton")
minimizeBtn.Size = UDim2.new(0,28,1,0)
minimizeBtn.Position = UDim2.new(1,-56,0,0)
minimizeBtn.Text = "-"
minimizeBtn.Font = Enum.Font.Code
minimizeBtn.TextSize = 18
minimizeBtn.BackgroundColor3 = Color3.fromRGB(60,60,60)
minimizeBtn.TextColor3 = Color3.new(1,1,1)
minimizeBtn.Parent = titleBar

local closeBtn = Instance.new("TextButton")
closeBtn.Size = UDim2.new(0,28,1,0)
closeBtn.Position = UDim2.new(1,-28,0,0)
closeBtn.Text = "X"
closeBtn.Font = Enum.Font.Code
closeBtn.TextSize = 16
closeBtn.BackgroundColor3 = Color3.fromRGB(120,40,40)
closeBtn.TextColor3 = Color3.new(1,1,1)
closeBtn.Parent = titleBar

-- Content container
local container = Instance.new("Frame")
container.Size = UDim2.new(1,-12,1,-34)
container.Position = UDim2.new(0,6,0,30)
container.BackgroundTransparency = 1
container.Parent = frame

-- Waypoint name input
local nameBox = Instance.new("TextBox")
nameBox.Size = UDim2.new(1,0,0,24)
nameBox.Position = UDim2.new(0,0,0,0)
nameBox.PlaceholderText = "Waypoint Name"
nameBox.Font = Enum.Font.Code
nameBox.TextSize = 14
nameBox.BackgroundColor3 = Color3.fromRGB(45,45,45)
nameBox.TextColor3 = Color3.new(1,1,1)
nameBox.Parent = container

-- Save button
local saveButton = Instance.new("TextButton")
saveButton.Size = UDim2.new(1,0,0,24)
saveButton.Position = UDim2.new(0,0,0,28)
saveButton.Text = "Save Waypoint"
saveButton.Font = Enum.Font.Code
saveButton.TextSize = 14
saveButton.BackgroundColor3 = Color3.fromRGB(255,140,0)
saveButton.TextColor3 = Color3.new(1,1,1)
saveButton.Parent = container

-- Movement label
local methodLabel = Instance.new("TextLabel")
methodLabel.Size = UDim2.new(1,0,0,18)
methodLabel.Position = UDim2.new(0,0,0,60)
methodLabel.BackgroundTransparency = 1
methodLabel.Text = "Movement Method"
methodLabel.Font = Enum.Font.Code
methodLabel.TextSize = 13
methodLabel.TextColor3 = Color3.new(1,1,1)
methodLabel.TextXAlignment = Enum.TextXAlignment.Left
methodLabel.Parent = container

-- Tween button
local tweenButton = Instance.new("TextButton")
tweenButton.Size = UDim2.new(0.48,0,0,24)
tweenButton.Position = UDim2.new(0,0,0,80)
tweenButton.Text = "Tween"
tweenButton.Font = Enum.Font.Code
tweenButton.TextSize = 13
tweenButton.BackgroundColor3 = Color3.fromRGB(70,70,70)
tweenButton.TextColor3 = Color3.new(1,1,1)
tweenButton.Parent = container

-- Teleport button
local tpButton = Instance.new("TextButton")
tpButton.Size = UDim2.new(0.48,0,0,24)
tpButton.Position = UDim2.new(0.52,0,0,80)
tpButton.Text = "Teleport"
tpButton.Font = Enum.Font.Code
tpButton.TextSize = 13
tpButton.BackgroundColor3 = Color3.fromRGB(70,70,70)
tpButton.TextColor3 = Color3.new(1,1,1)
tpButton.Parent = container

-- Tween speed box
local speedBox = Instance.new("TextBox")
speedBox.Size = UDim2.new(1,0,0,24)
speedBox.Position = UDim2.new(0,0,0,110)
speedBox.PlaceholderText = "Tween Speed"
speedBox.Font = Enum.Font.Code
speedBox.TextSize = 13
speedBox.BackgroundColor3 = Color3.fromRGB(45,45,45)
speedBox.TextColor3 = Color3.new(1,1,1)
speedBox.Parent = container

-- Go button
local goButton = Instance.new("TextButton")
goButton.Size = UDim2.new(1,0,0,24)
goButton.Position = UDim2.new(0,0,0,140)
goButton.Text = "Go To Selected"
goButton.Font = Enum.Font.Code
goButton.TextSize = 14
goButton.BackgroundColor3 = Color3.fromRGB(255,140,0)
goButton.TextColor3 = Color3.new(1,1,1)
goButton.Parent = container

-- Waypoint list
local listFrame = Instance.new("ScrollingFrame")
listFrame.Size = UDim2.new(1,0,1,-170)
listFrame.Position = UDim2.new(0,0,0,170)
listFrame.BackgroundColor3 = Color3.fromRGB(40,40,40)
listFrame.ScrollBarThickness = 6
listFrame.BorderSizePixel = 0
listFrame.CanvasSize = UDim2.new(0,0,0,0)
listFrame.Parent = container

local layout = Instance.new("UIListLayout")
layout.Padding = UDim.new(0,4)
layout.Parent = listFrame

-- Update movement UI
local function updateMethodUI()
	if movementMethod == "Tween" then
		tweenButton.BackgroundColor3 = Color3.fromRGB(120,120,120)
		tpButton.BackgroundColor3 = Color3.fromRGB(70,70,70)
	else
		tpButton.BackgroundColor3 = Color3.fromRGB(120,120,120)
		tweenButton.BackgroundColor3 = Color3.fromRGB(70,70,70)
	end
end

updateMethodUI()

tweenButton.MouseButton1Click:Connect(function()
	movementMethod = "Tween"
	updateMethodUI()
end)

tpButton.MouseButton1Click:Connect(function()
	movementMethod = "Teleport"
	updateMethodUI()
end)

-- Marker GUI
local function createMarker(position,name)
	local attachment = Instance.new("Attachment")
	attachment.WorldPosition = position + Vector3.new(0,2,0)
	attachment.Parent = workspace.Terrain

	local billboard = Instance.new("BillboardGui")
	billboard.Size = UDim2.new(0,100,0,40)
	billboard.AlwaysOnTop = true
	billboard.Parent = attachment

	local dot = Instance.new("Frame")
	dot.Size = UDim2.new(0,6,0,6)
	dot.Position = UDim2.new(0.5,-3,0.65,0)
	dot.BackgroundColor3 = Color3.new(1,1,1)
	dot.BorderSizePixel = 0
	dot.Parent = billboard

	local label = Instance.new("TextLabel")
	label.Size = UDim2.new(1,0,0.5,0)
	label.BackgroundTransparency = 1
	label.Text = name
	label.TextSize = 10
	label.Font = Enum.Font.Code
	label.TextColor3 = Color3.new(1,1,1)
	label.Parent = billboard

	return attachment
end

-- Add waypoint button
local function addWaypointButton(data)
	local button = Instance.new("TextButton")
	button.Size = UDim2.new(1,0,0,24)
	button.Text = data.name
	button.Font = Enum.Font.Code
	button.TextSize = 13
	button.BackgroundColor3 = Color3.fromRGB(60,60,60)
	button.TextColor3 = Color3.new(1,1,1)
	button.Parent = listFrame

	button.MouseButton1Click:Connect(function()
		selectedWaypoint = data

		if selectedButton then
			selectedButton.BackgroundColor3 = Color3.fromRGB(60,60,60)
		end

		button.BackgroundColor3 = Color3.fromRGB(120,120,120)
		selectedButton = button
	end)

	listFrame.CanvasSize = UDim2.new(0,0,0,layout.AbsoluteContentSize.Y)
end

-- Save waypoint
saveButton.MouseButton1Click:Connect(function()
	local char = player.Character or player.CharacterAdded:Wait()
	local hrp = char:WaitForChild("HumanoidRootPart")

	local name = nameBox.Text ~= "" and nameBox.Text or ("Waypoint "..(#waypoints+1))
	local pos = hrp.Position

	local marker = createMarker(pos,name)
	local data = {name=name, position=pos, marker=marker}

	table.insert(waypoints,data)
	addWaypointButton(data)

	nameBox.Text = ""
end)

-- Go to waypoint
goButton.MouseButton1Click:Connect(function()
	if not selectedWaypoint then return end

	local char = player.Character or player.CharacterAdded:Wait()
	local hrp = char:WaitForChild("HumanoidRootPart")

	local target = selectedWaypoint.position

	if movementMethod == "Teleport" then
		hrp.CFrame = CFrame.new(target)
	else
		local speed = tonumber(speedBox.Text) or 16
		local dist = (hrp.Position - target).Magnitude
		local time = dist / speed

		local tween = TweenService:Create(
			hrp,
			TweenInfo.new(time,Enum.EasingStyle.Linear),
			{CFrame = CFrame.new(target)}
		)

		tween:Play()
	end
end)

-- Minimize
minimizeBtn.MouseButton1Click:Connect(function()
	minimized = not minimized
	container.Visible = not minimized
	frame.Size = minimized and UDim2.new(0,260,0,28) or UDim2.new(0,260,0,340)
end)

-- Close
closeBtn.MouseButton1Click:Connect(function()
	gui:Destroy()
end)
