CCCPaste
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local AstralEvent = ReplicatedStorage:WaitForChild("AstralEvent")
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera

local isFlying = false
local debounce = false

-- === ВСТАВЬ СЮДА СВОИ ID (ОБЯЗАТЕЛЬНО) ===
-- Если нет анимации, создай пустую и вставь её ID, иначе будет ошибка
local IDLE_ANIM_ID = "rbxassetid://ТВОЙ_ID_ТУТ" 
local MOVE_ANIM_ID = "rbxassetid://ТВОЙ_ID_ТУТ"

local FLIGHT_SPEED = 50
local KEY_BIND = Enum.KeyCode.G

local bodyGyro, bodyVel
local connection
local loadedIdleTrack = nil
local loadedMoveTrack = nil
local currentTrack = nil -- Текущий играющий трек

local controls = {W = 0, S = 0, A = 0, D = 0, Space = 0, Ctrl = 0}

-- Загрузчик анимаций
local function setupAnimations(animator)
	local idleAnim = Instance.new("Animation")
	idleAnim.AnimationId = IDLE_ANIM_ID
	
	local moveAnim = Instance.new("Animation")
	moveAnim.AnimationId = MOVE_ANIM_ID

	loadedIdleTrack = animator:LoadAnimation(idleAnim)
	loadedIdleTrack.Priority = Enum.AnimationPriority.Action -- Самый высокий приоритет
	loadedIdleTrack.Looped = true

	loadedMoveTrack = animator:LoadAnimation(moveAnim)
	loadedMoveTrack.Priority = Enum.AnimationPriority.Action
	loadedMoveTrack.Looped = true
end

local function playAnim(trackName)
	if trackName == "Idle" then
		if not loadedIdleTrack.IsPlaying then
			if loadedMoveTrack.IsPlaying then loadedMoveTrack:Stop(0.2) end
			loadedIdleTrack:Play(0.2)
			print("Запуск анимации: IDLE")
		end
	elseif trackName == "Move" then
		if not loadedMoveTrack.IsPlaying then
			if loadedIdleTrack.IsPlaying then loadedIdleTrack:Stop(0.2) end
			loadedMoveTrack:Play(0.2)
			print("Запуск анимации: MOVE")
		end
	end
end

local function stopAllAnims()
	if loadedIdleTrack then loadedIdleTrack:Stop() end
	if loadedMoveTrack then loadedMoveTrack:Stop() end
end

local function startFlying(char)
	local rootPart = char:WaitForChild("HumanoidRootPart")
	local humanoid = char:WaitForChild("Humanoid")
	local animator = humanoid:WaitForChild("Animator")

	-- Инициализация анимаций при старте полета
	setupAnimations(animator)
	
	humanoid.PlatformStand = true 
	
	bodyGyro = Instance.new("BodyGyro")
	bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
	bodyGyro.P = 10000
	bodyGyro.CFrame = rootPart.CFrame
	bodyGyro.Parent = rootPart
	
	bodyVel = Instance.new("BodyVelocity")
	bodyVel.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	bodyVel.Velocity = Vector3.new(0, 0, 0)
	bodyVel.Parent = rootPart
	
	-- Сразу играем Idle
	playAnim("Idle")
	
	connection = RunService.RenderStepped:Connect(function()
		if not char or not rootPart then return end
		
		bodyGyro.CFrame = camera.CFrame
		
		local forward = camera.CFrame.LookVector * (controls.W - controls.S)
		local right = camera.CFrame.RightVector * (controls.D - controls.A)
		local up = Vector3.new(0, 1, 0) * (controls.Space - controls.Ctrl)
		local direction = (forward + right + up)
		
		bodyVel.Velocity = direction * FLIGHT_SPEED
		
		-- Управление анимациями
		if direction.Magnitude > 0.1 then
			playAnim("Move")
		else
			playAnim("Idle")
		end
		
		-- Noclip
		for _, part in pairs(char:GetDescendants()) do
			if part:IsA("BasePart") then part.CanCollide = false end
		end
	end)
end

local function stopFlying(char)
	if connection then connection:Disconnect() end
	if bodyGyro then bodyGyro:Destroy() end
	if bodyVel then bodyVel:Destroy() end
	
	stopAllAnims()
	
	local humanoid = char:FindFirstChild("Humanoid")
	if humanoid then humanoid.PlatformStand = false end
	
	for _, part in pairs(char:GetDescendants()) do
		if part:IsA("BasePart") then part.CanCollide = true end
	end
end

UserInputService.InputBegan:Connect(function(input, gp)
	if gp then return end
	if input.KeyCode == Enum.KeyCode.W then controls.W = 1
	elseif input.KeyCode == Enum.KeyCode.S then controls.S = 1
	elseif input.KeyCode == Enum.KeyCode.A then controls.A = 1
	elseif input.KeyCode == Enum.KeyCode.D then controls.D = 1
	elseif input.KeyCode == Enum.KeyCode.Space then controls.Space = 1
	elseif input.KeyCode == Enum.KeyCode.LeftControl then controls.Ctrl = 1
	elseif input.KeyCode == KEY_BIND then
		if not debounce then
			debounce = true
			local char = player.Character
			if char then
				isFlying = not isFlying
				AstralEvent:FireServer()
				if isFlying then startFlying(char) else stopFlying(char) end
			end
			task.wait(1)
			debounce = false
		end
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.W then controls.W = 0
	elseif input.KeyCode == Enum.KeyCode.S then controls.S = 0
	elseif input.KeyCode == Enum.KeyCode.A then controls.A = 0
	elseif input.KeyCode == Enum.KeyCode.D then controls.D = 0
	elseif input.KeyCode == Enum.KeyCode.Space then controls.Space = 0
	elseif input.KeyCode == Enum.KeyCode.LeftControl then controls.Ctrl = 0
	end
end)