------------------------------------------------------------------------------
--------------------[ MAIN PROGRAM ]------------------------------------------
------------------------------------------------------------------------------


while true do
	if (not Main:IsDescendantOf(game))
	or (Main.Parent.Name == "Knife" and FindFirstClass(Main, "Weld"))
	or Main.Anchored then
		break
	end
	
	if S.GrenadeTrail and S.GrenadeTransparency ~= 1
	and S.GrenadeTrailThickness ~= 0 and S.GrenadeTrailVisibleTime ~= 0 then
		local Trail = Instance.new("Part")
		Trail.BrickColor = S.GrenadeTrailColor
		Trail.Transparency = S.GrenadeTrailTransparency
		Trail.Anchored = true
		Trail.CanCollide = false
		Trail.Size = VEC3(1, 1, 1)
		local Mesh = Instance.new("BlockMesh")
		Mesh.Offset = VEC3(0, 0, -(Main.Position - LastPos).magnitude / 2)
		Mesh.Scale = VEC3(S.GrenadeTrailThickness, S.GrenadeTrailThickness, (Main.Position - LastPos).magnitude)
		Mesh.Parent = Trail
		Trail.Parent = Main.Parent
		Trail.CFrame = CF(LastPos, Main.Position)
		delay(S.GrenadeTrailVisibleTime, function()
			if S.GrenadeTrailDisappearTime > 0 then
				local X = 0
				while true do
					if X == 90 then break end
					local NewX = X + (3 / S.GrenadeTrailDisappearTime)
					X = (NewX > 90 and 90 or NewX)
					local Alpha = X / 90
					Trail.Transparency = NumLerp(S.GrenadeTrailTransparency, 1, Alpha)
					wait()
				end
				Trail:Destroy()
			else
				Trail:Destroy()
			end
		end)
		LastPos = Main.Position
	end
	
	wait()
end

if (not Main:IsDescendantOf(game)) then
	Grenade:Destroy()
end
