local Players = game:GetService("Players") local RunService = game:GetService("RunService") local player = Players.LocalPlayer local camera = workspace.CurrentCamera -- Config local TRIGGER_IDS = { ["114924283270252"] = true, ["130490463850590"] = true, } local FORWARD_SPEED = 90 -- studs/s (forward and backward) local SIDE_SPEED = 60 -- studs/s (left and right) -- WASD, gamepad and mobile joystick all go through the default controls module local controls = require(player:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule")):GetControls() local runConnection -- per-character Heartbeat connection local function isTriggerPlaying(animator) for _, track in ipairs(animator:GetPlayingAnimationTracks()) do local id = tostring(track.Animation.AnimationId):match("%d+") if id and TRIGGER_IDS[id] then return true end end return false end local function setupCharacter(character) if runConnection then runConnection:Disconnect() runConnection = nil end local humanoid = character:WaitForChild("Humanoid") local root = character:WaitForChild("HumanoidRootPart") local animator = humanoid:WaitForChild("Animator") local flying = false local attachment, linearVelocity, align local function activate() flying = true attachment = Instance.new("Attachment") attachment.Name = "FlightAttachment" attachment.Parent = root linearVelocity = Instance.new("LinearVelocity") linearVelocity.Attachment0 = attachment linearVelocity.RelativeTo = Enum.ActuatorRelativeTo.World linearVelocity.MaxForce = math.huge linearVelocity.VectorVelocity = Vector3.zero linearVelocity.Parent = root align = Instance.new("AlignOrientation") align.Mode = Enum.OrientationAlignmentMode.OneAttachment align.Attachment0 = attachment align.MaxTorque = math.huge align.Responsiveness = 30 align.CFrame = root.CFrame align.Parent = root humanoid.PlatformStand = true end local function deactivate() flying = false if linearVelocity then linearVelocity:Destroy() linearVelocity = nil end if align then align:Destroy() align = nil end if attachment then attachment:Destroy() attachment = nil end if humanoid.Parent then humanoid.PlatformStand = false end end runConnection = RunService.Heartbeat:Connect(function() if humanoid.Health <= 0 then if flying then deactivate() end return end local shouldFly = isTriggerPlaying(animator) if shouldFly and not flying then activate() elseif not shouldFly and flying then deactivate() end if flying then -- x = right(+)/left(-), z = backward(+)/forward(-), relative to the camera local move = controls:GetMoveVector() local camCF = camera.CFrame local velocity = camCF.RightVector * (move.X * SIDE_SPEED) + camCF.LookVector * (-move.Z * FORWARD_SPEED) linearVelocity.VectorVelocity = velocity -- Face the direction the camera is looking align.CFrame = CFrame.lookAt(root.Position, root.Position + camCF.LookVector) end end) humanoid.Died:Once(function() if flying then deactivate() end if runConnection then runConnection:Disconnect() runConnection = nil end end) end player.CharacterAdded:Connect(setupCharacter) if player.Character then task.spawn(setupCharacter, player.Character) end