Tool=script.Parent Handle=Tool:WaitForChild("\072\097\110\100\108\101")Players=game:GetService("\080\108\097\121\101\114\115")RunService=game:GetService("\082\117\110\083\101\114\118\105\099\101")Camera=(game:GetService("\087\111\114\107\115\112\097\099\101")).CurrentCamera Animations={}LocalObjects={}ServerControl=Tool:WaitForChild("\083\101\114\118\101\114\067\111\110\116\114\111\108")ClientControl=Tool:WaitForChild("\067\108\105\101\110\116\067\111\110\116\114\111\108")Rate=.016666666666667 CameraSpeed={X=40,Z=60}Controls={Forward={Mode=false,Keys={Key="\119",ByteKey=17}};Backward={Mode=false;Keys={Key="\115",ByteKey=18}},Left={Mode=false,Keys={Key="\097";ByteKey=20}},Right={Mode=false;Keys={Key="\100",ByteKey=19}}}ToolEquipped=false function HandleFlightControl()if not CheckIfAlive()then return end if FightMonitor then FightMonitor:disconnect()end FightMonitor=Torso.ChildAdded:connect(function(s)if Flying then return end if s.Name=="\070\108\105\103\104\116\072\111\108\100"then local s=Torso:FindFirstChild("\070\108\105\103\104\116\083\112\105\110")local m=Torso:FindFirstChild("\070\108\105\103\104\116\080\111\119\101\114")local h=Torso:FindFirstChild("\070\108\105\103\104\116\072\111\108\100")if not s or not m or not h then return end Flying=true Humanoid.WalkSpeed=0 Humanoid.PlatformStand=true Humanoid.AutoRotate=false DisableJump(true)Torso.Velocity=Vector3.new(0,0,0)Torso.RotVelocity=Vector3.new(0,0,0)while Flying and(s.Parent and(m.Parent and(h.Parent and CheckIfAlive())))do local f=Vector3.new(0,0,0)local S=Camera.CoordinateFrame:vectorToWorldSpace(Vector3.new(0,0,-1))local I=Camera.CoordinateFrame:vectorToWorldSpace(Vector3.new(-1,0,0))f=f+((Controls.Forward.Mode and not Controls.Backward.Mode)and S*CameraSpeed.Z or(not Controls.Forward.Mode and Controls.Backward.Mode)and(S*CameraSpeed.Z)*-1 or f)f=f+((Controls.Left.Mode and not Controls.Right.Mode)and I*CameraSpeed.X or(not Controls.Left.Mode and Controls.Right.Mode)and(I*CameraSpeed.X)*-1 or f)s.cframe=CFrame.new(Vector3.new(0,0,0),S)if f.magnitude<1 then h.maxForce=Vector3.new(h.P,h.P,h.P)m.maxForce=Vector3.new(0,0,0)h.position=Torso.Position else h.maxForce=Vector3.new(0,0,0)m.maxForce=Vector3.new(m.P*100,m.P*100,m.P*100)end m.velocity=f wait(Rate)end Flying=false if CheckIfAlive()then Torso.Velocity=Vector3.new(0,0,0)Torso.RotVelocity=Vector3.new(0,0,0)Humanoid.WalkSpeed=16 Humanoid.PlatformStand=false Humanoid.AutoRotate=true DisableJump(false)Humanoid:ChangeState(Enum.HumanoidStateType.Freefall)end end end)end function SetAnimation(s,m)if s=="\080\108\097\121\065\110\105\109\097\116\105\111\110"and(m and(ToolEquipped and Humanoid))then for s,h in pairs(Animations)do if h.Animation==m.Animation then h.AnimationTrack:Stop()table.remove(Animations,s)end end local s=Humanoid:LoadAnimation(m.Animation)table.insert(Animations,{Animation=m.Animation,AnimationTrack=s})s:Play(m.FadeTime,m.Weight,m.Speed)elseif s=="\083\116\111\112\065\110\105\109\097\116\105\111\110"and m then for s,h in pairs(Animations)do if h.Animation==m.Animation then h.AnimationTrack:Stop()table.remove(Animations,s)end end end end function DisableJump(s)if PreventJump then PreventJump:disconnect()end if s then PreventJump=Humanoid.Changed:connect(function(s)if s=="\074\117\109\112"then Humanoid.Jump=false end end)end end function CheckIfAlive()return(Character and(Character.Parent and(Humanoid and(Humanoid.Parent and(Humanoid.Health>0 and(Torso and(Torso.Parent and(Player and Player.Parent))))))))and true or false end function KeyPress(s,m)local h=string.lower(s)local f=string.byte(h)for s,S in pairs(Controls)do if h==S.Keys.Key or f==S.Keys.ByteKey then Controls[s].Mode=m end end end function Equipped(s)Character=Tool.Parent Player=Players:GetPlayerFromCharacter(Character)Humanoid=Character:FindFirstChild("\072\117\109\097\110\111\105\100")Torso=Character:FindFirstChild("\084\111\114\115\111")ToolEquipped=true if not CheckIfAlive()then return end s.KeyDown:connect(function(s)KeyPress(s,true)end)s.KeyUp:connect(function(s)KeyPress(s,false)end)Spawn(HandleFlightControl)end function Unequipped()Flying=false LocalObjects={}for s,m in pairs(Animations)do if m and m.AnimationTrack then m.AnimationTrack:Stop()end end for s,m in pairs({PreventJump;FightMonitor})do if m then m:disconnect()end end for s,m in pairs(Controls)do Controls[s].Mode=false end Animations={}ToolEquipped=false end function InvokeServer(s,m)local h pcall(function()h=ServerControl:InvokeServer(s,m)end)return h end function OnClientInvoke(s,m)if s=="\080\108\097\121\065\110\105\109\097\116\105\111\110"and(m and(ToolEquipped and Humanoid))then SetAnimation("\080\108\097\121\065\110\105\109\097\116\105\111\110",m)elseif s=="\083\116\111\112\065\110\105\109\097\116\105\111\110"and m then SetAnimation("\083\116\111\112\065\110\105\109\097\116\105\111\110",m)elseif s=="\080\108\097\121\083\111\117\110\100"and m then m:Play()elseif s=="\083\116\111\112\083\111\117\110\100"and m then m:Stop()end end ClientControl.OnClientInvoke=OnClientInvoke Tool.Equipped:connect(Equipped)Tool.Unequipped:connect(Unequipped)


Tool = script.Parent
Handle = Tool:WaitForChild("Handle")

Players = game:GetService("Players")
RunService = game:GetService("RunService")

Camera = game:GetService("Workspace").CurrentCamera

Animations = {}
LocalObjects = {}

ServerControl = Tool:WaitForChild("ServerControl")
ClientControl = Tool:WaitForChild("ClientControl")

Rate = (1 / 60)

CameraSpeed = {
	X = 40,
	Z = 60
}

Controls = {
	Forward = {
		Mode = false,
		Keys = {Key = "w", ByteKey = 17}
	},
	Backward = {
		Mode = false,
		Keys = {Key = "s", ByteKey = 18}
	},
	Left = {
		Mode = false,
		Keys = {Key = "a", ByteKey = 20}
	},
	Right = {
		Mode = false,
		Keys = {Key = "d", ByteKey = 19}
	},
}

ToolEquipped = false

function HandleFlightControl()
	if not CheckIfAlive() then
		return
	end
	if FightMonitor then
		FightMonitor:disconnect()
	end
	FightMonitor = Torso.ChildAdded:connect(function(Child)
		if Flying then
			return
		end
		if Child.Name == "FlightHold" then
			local FlightSpin = Torso:FindFirstChild("FlightSpin")
			local FlightPower = Torso:FindFirstChild("FlightPower")
			local FlightHold = Torso:FindFirstChild("FlightHold")
			if not FlightSpin or not FlightPower or not FlightHold then
				return
			end
			Flying = true
			Humanoid.WalkSpeed = 0
			Humanoid.PlatformStand = true
			Humanoid.AutoRotate = false
			DisableJump(true)
			Torso.Velocity = Vector3.new(0, 0, 0)
			Torso.RotVelocity = Vector3.new(0, 0, 0)
			while Flying and FlightSpin.Parent and FlightPower.Parent and FlightHold.Parent and CheckIfAlive() do
				local NewPush = Vector3.new(0, 0, 0)
				local ForwardVector = Camera.CoordinateFrame:vectorToWorldSpace(Vector3.new(0, 0, -1))
				local SideVector = Camera.CoordinateFrame:vectorToWorldSpace(Vector3.new(-1, 0, 0))
				NewPush = (NewPush + (((Controls.Forward.Mode and not Controls.Backward.Mode) and (ForwardVector * CameraSpeed.Z)) or ((not Controls.Forward.Mode and Controls.Backward.Mode) and (ForwardVector * CameraSpeed.Z * -1)) or NewPush))
				NewPush = (NewPush + (((Controls.Left.Mode and not Controls.Right.Mode) and (SideVector * CameraSpeed.X)) or ((not Controls.Left.Mode and Controls.Right.Mode) and (SideVector * CameraSpeed.X * -1)) or NewPush))
				FlightSpin.cframe = CFrame.new(Vector3.new(0, 0, 0), ForwardVector)
				if NewPush.magnitude < 1 then
					FlightHold.maxForce = Vector3.new(FlightHold.P, FlightHold.P, FlightHold.P)
					FlightPower.maxForce = Vector3.new(0, 0, 0)
					FlightHold.position = Torso.Position
				else
					FlightHold.maxForce = Vector3.new(0, 0, 0)
					FlightPower.maxForce = Vector3.new((FlightPower.P * 100), (FlightPower.P * 100), (FlightPower.P * 100))
				end
				FlightPower.velocity = NewPush
				wait(Rate)
			end
			Flying = false
			if CheckIfAlive() then
				Torso.Velocity = Vector3.new(0, 0, 0)
				Torso.RotVelocity = Vector3.new(0, 0, 0)
				Humanoid.WalkSpeed = 16
				Humanoid.PlatformStand = false
				Humanoid.AutoRotate = true
				DisableJump(false)
				Humanoid:ChangeState(Enum.HumanoidStateType.Freefall)
			end
		end
	end)
end

function SetAnimation(mode, value)
	if mode == "PlayAnimation" and value and ToolEquipped and Humanoid then
		for i, v in pairs(Animations) do
			if v.Animation == value.Animation then
				v.AnimationTrack:Stop()
				table.remove(Animations, i)
			end
		end
		local AnimationTrack = Humanoid:LoadAnimation(value.Animation)
		table.insert(Animations, {Animation = value.Animation, AnimationTrack = AnimationTrack})
		AnimationTrack:Play(value.FadeTime, value.Weight, value.Speed)
	elseif mode == "StopAnimation" and value then
		for i, v in pairs(Animations) do
			if v.Animation == value.Animation then
				v.AnimationTrack:Stop()
				table.remove(Animations, i)
			end
		end
	end
end

function DisableJump(Boolean)
	if PreventJump then
		PreventJump:disconnect()
	end
	if Boolean then
		PreventJump = Humanoid.Changed:connect(function(Property)
			if Property ==  "Jump" then
				Humanoid.Jump = false
			end
		end)
	end
end

function CheckIfAlive()
	return (((Character and Character.Parent and Humanoid and Humanoid.Parent and Humanoid.Health > 0 and Torso and Torso.Parent and Player and Player.Parent) and true) or false)
end

function KeyPress(Key, Down)
	local Key = string.lower(Key)
	local ByteKey = string.byte(Key)
	for i, v in pairs(Controls) do
		if Key == v.Keys.Key or ByteKey == v.Keys.ByteKey then
			Controls[i].Mode = Down
		end
	end
end

function Equipped(Mouse)
	Character = Tool.Parent
	Player = Players:GetPlayerFromCharacter(Character)
	Humanoid = Character:FindFirstChild("Humanoid")
	Torso = Character:FindFirstChild("Torso")
	ToolEquipped = true
	if not CheckIfAlive() then
		return
	end
	Mouse.KeyDown:connect(function(Key)
		KeyPress(Key, true)
	end)
	Mouse.KeyUp:connect(function(Key)
		KeyPress(Key, false)
	end)
	Spawn(HandleFlightControl)
end

function Unequipped()
	Flying = false
	LocalObjects = {}
	for i, v in pairs(Animations) do
		if v and v.AnimationTrack then
			v.AnimationTrack:Stop()
		end
	end
	for i, v in pairs({PreventJump, FightMonitor}) do
		if v then
			v:disconnect()
		end
	end
	for i, v in pairs(Controls) do
		Controls[i].Mode = false
	end
	Animations = {}
	ToolEquipped = false
end

function InvokeServer(mode, value)
	local ServerReturn
	pcall(function()
		ServerReturn = ServerControl:InvokeServer(mode, value)
	end)
	return ServerReturn
end

function OnClientInvoke(mode, value)
	if mode == "PlayAnimation" and value and ToolEquipped and Humanoid then
		SetAnimation("PlayAnimation", value)
	elseif mode == "StopAnimation" and value then
		SetAnimation("StopAnimation", value)
	elseif mode == "PlaySound" and value then
		value:Play()
	elseif mode == "StopSound" and value then
		value:Stop()
	end
end

ClientControl.OnClientInvoke = OnClientInvoke
Tool.Equipped:connect(Equipped)
Tool.Unequipped:connect(Unequipped)
