--Made by Luckymaxer


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

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

BaseUrl = "http://www.roblox.com/asset/?id="

RbxUtility = LoadLibrary("RbxUtility")
Create = RbxUtility.Create

Particles = script:WaitForChild("Particles")

BasePart = Create("Part"){
	Shape = Enum.PartType.Block,
	Material = Enum.Material.Plastic,
	TopSurface = Enum.SurfaceType.Smooth,
	BottomSurface = Enum.SurfaceType.Smooth,
	FormFactor = Enum.FormFactor.Custom,
	Size = Vector3.new(0.2, 0.2, 0.2),
	CanCollide = true,
	Locked = true,
	Anchored = false,
}

Animations = {
	Fly = {Animation = Tool:WaitForChild("Fly"), FadeTime = nil, Weight = nil, Speed = nil, Duration = nil}
}

Sounds = {}

FogParticles = {}

Remotes = Tool:WaitForChild("Remotes")
ServerControl = (Remotes:FindFirstChild("ServerControl") or Create("RemoteFunction"){
	Name = "ServerControl",
	Parent = Remotes,
})

ClientControl = (Remotes:FindFirstChild("ClientControl") or Create("RemoteFunction"){
	Name = "ClientControl",
	Parent = Remotes,
})

ReloadTime = 1

ToolEquipped = false

Handle.Transparency = 0
Tool.Enabled = true

function Activated()
	local MouseData = InvokeClient("MouseData")
	if not MouseData or not MouseData.Position then
		return
	end
	if not Tool.Enabled or not CheckIfAlive() or not ToolEquipped then
		return
	end
	Tool.Enabled = false
	wait(ReloadTime)
	Tool.Enabled = true
end

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

function Equipped()
	Character = Tool.Parent
	Player = Players:GetPlayerFromCharacter(Character)
	Humanoid = Character:FindFirstChild("Humanoid")
	RootPart = Character:FindFirstChild("HumanoidRootPart")
	if not CheckIfAlive() then
		return
	end
	ToolEquipped = true
end

function Unequipped()
	if FogLoop then
		FogLoop:disconnect()
	end
	for i, v in pairs(FogParticles) do
		if v and v.Parent then
			v.Enabled = false
			Debris:AddItem(v, 3)
		end
	end
	FogParticles = {}
	ToolEquipped = false
end

function InvokeClient(Mode, Value)
	local ClientReturn = nil
	pcall(function()
		ClientReturn = ClientControl:InvokeClient(Player, Mode, Value)
	end)
	return ClientReturn
end

function OnServerInvoke(player, Mode, Value)
	if player ~= Player or not ToolEquipped or not CheckIfAlive() or not Mode or not Value then
		return
	end
	if Mode == "Flying" then
		local Mode = Value.Mode
		for i, v in pairs(FogParticles) do
			if v and v.Parent then
				v.Enabled = false
				Debris:AddItem(v, 3)
			end
		end
		FogParticles = {}
		if FogLoop then
			FogLoop:disconnect()
		end
		if Mode then
			Spawn(function()
				InvokeClient("PlayAnimation", Animations.Fly)
			end)
			for i, v in pairs(Particles:GetChildren()) do
				if v:IsA("ParticleEmitter") then
					local Particle = v:Clone()
					table.insert(FogParticles, Particle)
					Particle.Enabled = false
					Particle.Parent = RootPart
				end
			end
			FogLoop = RunService.Stepped:connect(function(Time, Step)
				local Speed = (RootPart.Velocity * Vector3.new(1, 0, 1)).Magnitude
				for i, v in pairs(FogParticles) do
					v.Enabled = (Speed > 15)
				end
			end)
		else
			Spawn(function()
				InvokeClient("StopAnimation", Animations.Fly)
			end)
		end
	end
end

ServerControl.OnServerInvoke = OnServerInvoke

Tool.Activated:connect(Activated)
Tool.Equipped:connect(Equipped)
Tool.Unequipped:connect(Unequipped)
